使用regex-php操作img src


manipulating img src with regex -php

我该如何替换这个:

http://example.com/myfolder/files/year/month/imagename-widthxheight.imageextension

到此:

http://example.com/myfolder/files/year/month/imagename-300x300.imageextension

有什么帮助吗?

你可能想试试这个

// if your src has widthxheight are specified literally like that you may try
echo preg_replace("/'W{0,1}(width).*(height)/i","-300x300","http://www.mysite.com/myfolder/files/year/month/imagename-widthxheight.imageextension");
// if your src has widthxheight are specified in int val you may try     
echo preg_replace("/'W{0,1}('d{1,7}).*('d{1,7})/i","-300x300","http://www.mysite.com/myfolder/files/year/month/imagename-123x456.imageextension");

-300x300的实际值将根据您的实际需求而有所不同。所以我认为最好通过变量传递这些值。

怎么样:

$new_img = preg_replace("~([^/]+)-widthxheight('.[^.]+)$~i","$1-300x300$2", 
"http://www.mysite.com/myfolder/files/year/month/imagename-widthxheight.imageextension");