当前位置: 动力学知识库 > 问答 > 编程问答 >

manipulating img src with regex -php

问题描述:

How would I replace this:

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

to this:

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

any help?

网友答案:

You may want to try this

// 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");

actual value of -300x300 will vary on your actual requirement. So I think its best to pass on these values through variables.

网友答案:

How about:

$new_img = preg_replace("~([^/]+)-widthxheight(\.[^.]+)$~i","$1-300x300$2", 
"http://www.mysite.com/myfolder/files/year/month/imagename-widthxheight.imageextension");
分享给朋友:
您可能感兴趣的文章:
随机阅读: