[英]PHP: change [img]src[/img] to <img src=“src” alt=“src” > with regular expression
[英]Regular expression to find “src” attribute of HTML “img” element in PHP
我有一个字符串,里面有一个图像:
"<p><img src="http://yahoo.com/testfolder/userdata/editoruploadimages/confused man.jpg" /></p>"
我无法使用正则表达式获取图像URL。 我的代码是:
preg_match_all("/src=([^\\s]+)/", $questArr_str, $images);
此代码在遇到映像名称中的空格时停止执行。 它只返回"http://yahoo.com/testfolder/userdata/editoruploadimages/confused
返回的字符串应为: "http://yahoo.com/testfolder/userdata/editoruploadimages/confused man.jpg"
我会抓住引号内的所有内容:
preg_match_all('/src="([^"]+)"/', $questArr_str, $images);
读取的部分([^\\s]+)
表示选择任何不是空格的东西。
也许尝试类似的东西:
/src="([^"]+)"/
哪个是选择任何不是双引号的东西。
感谢每一个人帮助我。
我找到了我的解决方案:
pattern = "/src=([^\\\"]+)/"
这是一种简单的方法来匹配<img />
标签src
属性和/或html/PHP
内容与正则表达式。
样品:
<img class="img img-responsive" title="publisher.PNG" src="media/projectx/agent/author/post/2/image/publisher.PNG" alt="" width="80%" />
仅匹配src
属性内容使用
preg_match("%(?<=src=\")([^\"])+(png|jpg|gif)%i",$input,$result)
$result[0]
将输出media/projectx/agent/author/post/2/image/publisher.PNG
匹配`src'属性和它的内容使用
preg_match("%src=\"([^\"])+(png|jpg|gif)\"%i",$input,$result)
$result[0]
将输出src="media/projectx/agent/author/post/2/image/publisher.PNG"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.