如何在PHP中找到字符串中的HTML标记?
这是我的数据库中的记录:
$str = "This is me <img src='images/mardagz.png' width='200' /> :) when i was 4th year highschool hahah so funny...";
Run Code Online (Sandbox Code Playgroud)
我正在尝试获取<img>标签的内容.这是我正在使用的代码:
$gimg = preg_match('/<img[^>]+\>/i', $str , $matches) ? $matches[1]: '<img src="http://facebook.com/username/profile.png" width="200" />
Run Code Online (Sandbox Code Playgroud)
但是,这段代码总是给我这个错误: Notice: Undefined offset: 1 in mardagz.blog\post.php on line 19
我该怎么办?
Fer*_*yer 11
$matches[1]保存与搜索模式中第一个组匹配的文本,但您的模式没有任何组.组用括号定义.
要么将整个模式放在这样的组中:
preg_match('/(<img[^>]+>)/i', $str, $matches)
Run Code Online (Sandbox Code Playgroud)
或者只是$matches[0]用来获得匹配的整个文本.