dsdukbc60905239
2014-02-11 21:59
浏览 39
已采纳

正则表达式:使用PHP查找字符串中的图像URL

I have a very complex string and i want to find and replace all urls that end with .jpg, .png, or .gif with a static url. For example;

$string = "Lorem ipsum dolor sit amet, http://www.website.com/images/some.jpg; consul delicata comprehensam eos at. Mea rebum laudem deterruisset et, ex epicuri constituto his. Sea ad appareat democritum. http://www.anotherwebsite.com/images/some.png Omnis vituperata dissentiunt an duo. Sumo diceret lobortis at sed, singulis aliquando prodesset ex sit.";

$regex = ?;
$static_image_url= "http://new_image_url";

return preg_replace($regex, $static_image_url, $string);

Please note this string may be more complex than this, urls are dynamic and this is NOT HTML.

Thank you very much in advance.

Best Regards,

EDIT:

This one worked for this case;

return preg_replace('/https?:\/\/[^ ]+?(?:\.jpg|\.png|\.gif)/', $static_image_url, $string);

图片转代码服务由CSDN问答提供 功能建议

我有一个非常复杂的字符串,我想找到并替换以.jpg,.png结尾的所有网址, 或.gif与静态网址。 例如;

  $ string =“Lorem ipsum dolor sit amet,http://www.website.com/images/some.jpg; consul delicata comprehensam eos at.Mea 他们是一名海洋广告服装人员.http://www.anotherwebsite.com/images/some.png Omnis vituperata disntiunt an duo。Sumo diceret lobortis at sed,singulis aliquando prodesset ex sit。“; rebum laudem deterruisset et,ex epicuri constituto his。  
 
 $ regex = ?; 
 $ static_image_url =“http:// new_image_url”; 
 
return preg_replace($ regex,$ static_image_url,$ string); 
   
  
 

请注意,这个字符串可能比这个更复杂,网址是动态的,这不是HTML。

非常感谢您提前。 \ n

最好的问候,

编辑:

这个适用于此案例;

 <  code> return preg_replace('/ https?:\ / \ / [^] +?(?:\。jpg | \ .png | \ .gif)/',$ static_image_url,$ string); 
    
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dqfsbvd43312 2014-02-11 22:10
    已采纳

    This should replace the links as long as they don't have a space in them (since links clearly can't have spaces).

    $string = "Lorem ipsum dolor sit amet, http://www.website.com/images/some.jpg; consul delicata comprehensam eos at. Mea rebum laudem deterruisset et, ex epicuri constituto his. Sea ad appareat democritum. http://www.anotherwebsite.com/images/some.png Omnis vituperata dissentiunt an duo. Sumo diceret lobortis at sed, singulis aliquando prodesset ex sit.";
    $static_image_url= "http://new_image_url";
    return preg_replace('/https?:\/\/[^ ]+?(?:\.jpg|\.png|\.gif)/', $static_image_url, $string);
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题