簡體   English   中英

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM