简体   繁体   English

如何删除img src标签中的变量?

[英]How to remove variable in img src tag?

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 ? =>如何删除变量(最大宽度),如何修复?

if you want to get rid of query , just do that: 如果您想摆脱查询 ,只需执行以下操作:

$filename = 'http://thebox.vn/Uploaded/catmy/2013_04_23/couple_t2.jpg?maxwidth=480';
$parts = explode("?",$filename);
$filename = $parts[0];

you could do: 你可以做:

$filename = "http://thebox.vn/Uploaded/catmy/2013_04_23/couple_t2.jpg?maxwidth=480";
$filename = array_shift(explode('?', $filename));
echo $filename;

You may try this 你可以试试这个

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

DEMO. 演示。

Your current regular expression says: replace the last character of $filename with the empty string if that last character is the question mark character. 您当前的正则表达式说:如果最后一个字符是问号字符,则用空字符串替换$filename的最后一个字符。

Here is a fixed regular expression that works for your particular example: /\\?maxwidth=.*$/ 这是一个适用于您的特定示例的固定正则表达式:/ /\\?maxwidth=.*$/

There are many other expressions that could do the job for various circumstances. 还有许多其他表达式可以在各种情况下发挥作用。 However, perhaps it would be better to use PHP's parse_url() function to split the URL into its various parts and then just discard the parts that you do not care about and merge back into a string. 但是,也许最好使用PHP的parse_url()函数将URL拆分为各个部分,然后丢弃不需要的部分,然后合并回字符串。 For example: 例如:

$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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM