你不应该在 HTML 上使用正则表达式,那这个呢:?
$string = '<span class="introduction"><img alt="image" src="/picture.jpg" /></span>';
echo strip_tags($string, '<img>');
否则我会使用 HTML/XML 解析器
怎么样
"<img[^>]*>"
尝试使用 grep
kent$ echo '<span class="introduction">
quote> <img alt="image" src="/picture.jpg" />
quote> </span>
quote> '|grep -P "<img[^>]*>"
<img alt="image" src="/picture.jpg" />
preg_match('#(<img.*?>)#', $string, $results);
应该有效,结果是
$results[1]
我已经找到了这个解决方案
/<img ([^>"']*("[^"]*"|'[^']*')?[^>"']*)*>/
测试于
<other html elements like span or whatever><img src="asd>qwe" attr1='asd>qwe' attr2='as"dqwe' attr3="as'dqwe" ></other html elements like span or whatever>