要查找的正则表达式";src";HTML的属性";img";PHP中的元素
我有一个字符串,里面有一个图像:要查找的正则表达式";src";HTML的属性";img";PHP中的元素,php,html,regex,Php,Html,Regex,我有一个字符串,里面有一个图像: "<p><img src="http://yahoo.com/testfolder/userdata/editoruploadimages/confused man.jpg" /></p>" 此代码在遇到图像名称中的空格时停止执行。它只返回”http://yahoo.com/testfolder/userdata/editoruploadimages/confused 返回的字符串应为: ”http://yahoo.com/
"<p><img src="http://yahoo.com/testfolder/userdata/editoruploadimages/confused man.jpg" /></p>"
此代码在遇到图像名称中的空格时停止执行。它只返回”http://yahoo.com/testfolder/userdata/editoruploadimages/confused
返回的字符串应为:
”http://yahoo.com/testfolder/userdata/editoruploadimages/confused jpg“
读取([^\s]+)
的部分意味着选择任何不是空格的内容
也许可以尝试以下方式:
/src="([^"]+)"/
这就是选择任何不是双引号的内容。我会捕捉引号内的所有内容:
preg_match_all('/src="([^"]+)"/', $questArr_str, $images);
谢谢大家帮助我 我通过以下方法找到了解决方案:
pattern = "/src=([^\\\"]+)/"
下面是一种简单的方法,可以将
html/PHP
中的属性和/或其内容与正则表达式进行匹配
样本:
<img class="img img-responsive" title="publisher.PNG" src="media/projectx/agent/author/post/2/image/publisher.PNG" alt="" width="80%" />
$result[0]
将输出media/projectx/agent/author/post/2/image/publisher.PNG
要匹配'src'属性和it内容,请使用
preg_match("%(?<=src=\")([^\"])+(png|jpg|gif)%i",$input,$result)
preg_match("%src=\"([^\"])+(png|jpg|gif)\"%i",$input,$result)
$result[0]
将输出src=“media/projectx/agent/author/post/2/image/publisher.PNG“
您只在正则表达式中搜索非空白字符,为什么它要与空格匹配?正则表达式不是匹配html的最佳解决方案,最好使用html/dom解析器。只匹配第一种模式。preg\u match\u all
“搜索所有匹配的主题”-您不需要g
标志
preg_match("%src=\"([^\"])+(png|jpg|gif)\"%i",$input,$result)