0

I have a sample code:

$filename = 'http://thebox.vn/Uploaded/catmy/2013_04_23/couple_t2.jpg?maxwidth=480';

And I using this code to remove variable (maxwidth)

echo preg_replace('/(\?)$/', '', $filename)

=> How to remove variable (maxwidth), how to fix it ?

4

4 回答 4

1

如果你想摆脱 query,只需这样做:

$filename = 'http://thebox.vn/Uploaded/catmy/2013_04_23/couple_t2.jpg?maxwidth=480';
$parts = explode("?",$filename);
$filename = $parts[0];
于 2013-05-06T03:28:43.567 回答
0

您当前的正则表达式说:$filename如果最后一个字符是问号字符,则用空字符串替换最后一个字符。

这是适用于您的特定示例的固定正则表达式:/\?maxwidth=.*$/

还有许多其他表达式可以在各种情况下完成这项工作。但是,也许最好使用 PHP 的parse_url()函数将 URL 拆分为各个部分,然后丢弃您不关心的部分并合并回字符串。例如:

$filename = 'http://thebox.vn/Uploaded/catmy/2013_04_23/couple_t2.jpg?maxwidth=480';

// Parse the filename into parts.
$filename_parsed = parse_url( $filename );

// Merge the parsed filename back into a string,
// discarding any irrelevant parts.
$filename_merged = $filename_parsed[ 'scheme' ] . '://' . $filename_parsed[ 'host' ] . $filename_parsed[ 'path' ];

// Prints: http://thebox.vn/Uploaded/catmy/2013_04_23/couple_t2.jpg
echo $filename_merged;
于 2013-05-06T03:50:10.527 回答
0

你可以这样做:

$filename = "http://thebox.vn/Uploaded/catmy/2013_04_23/couple_t2.jpg?maxwidth=480";
$filename = array_shift(explode('?', $filename));
echo $filename;
于 2013-05-06T03:31:17.883 回答
0

你可以试试这个

$filename = 'http://thebox.vn/Uploaded/catmy/2013_04_23/couple_t2.jpg?maxwidth=480';
echo preg_replace("/\?[a-z]+=\d+/", '', $filename);

演示。

于 2013-05-06T03:37:26.303 回答