使用php preg_match_all提取信息

使用php preg_match_all提取信息,php,arrays,preg-match-all,trim,Php,Arrays,Preg Match All,Trim,我正在尝试修改脚本以提取信息: 有些页面包含以下内容,例如: <ul class="tags"> <li><div class="tag"><a href="http://www.url1.com" title="url1">title 1</a></div></li> <li><div class="tag"><a href="http://www.url2.com" title=

我正在尝试修改脚本以提取信息:

有些页面包含以下内容,例如:

<ul class="tags">
<li><div class="tag"><a href="http://www.url1.com" title="url1">title 1</a></div></li>
<li><div class="tag"><a href="http://www.url2.com" title="url2">title 2</a></div></li>
</ul>
我想得到标题1,标题2

我有以下代码:

$m=array();
preg_match_all("/<a href="(.*)" title="(.*)">(.*)<\/a>/i", $buff,$m);
$info['tags']=trim(strip_tags($m[3]));
$cats=array_map('trim',$cats);
$info['tags']=implode(',',$cats);
但是修剪和数组映射会出错


谢谢您的帮助。

代码中的第一个错误在第三行:

$info['tags']=trim(strip_tags($m[3]));
在这里,函数strip_标记应该以字符串作为参数,但$m[3]是数组。快速解决办法是:

$info['tags']=array_map('strip_tags',array_map('trim',$m[3]));
$info[tags]现在是:

[tags] => Array
   (
       [0] => title 1
       [1] => title 2
   )
然后应用内爆函数,以便:

$info['tags'] = implode(',', $info['tags']);

干杯,Bea

我建议使用HTML解析器,尤其是DOMDocument,我希望$info[tags]是title 1,title 2在这种情况下,您可以使用内爆函数:$info[tags]=内爆,,$info[tags],结果将是title 1,title 2