Php 使用preg_match_all筛选<;a>;标记文本

Php 使用preg_match_all筛选<;a>;标记文本,php,html,regex,Php,Html,Regex,我正在尝试从这个链接获取文本 完整标签 <a href="/wiki/Correa_(apellido)" title="Correa (apellido)">Correa</a> 我的代码 $html = file_get_contents("https://es.wikipedia.org/wiki/Anexo:Apellidos_m%C3%A1s_comunes_en_Espa%C3%B1a_e_Hispanoam%C3%A9rica"); preg_mat

我正在尝试从这个链接获取文本

完整标签

<a href="/wiki/Correa_(apellido)" title="Correa (apellido)">Correa</a>

我的代码

$html = file_get_contents("https://es.wikipedia.org/wiki/Anexo:Apellidos_m%C3%A1s_comunes_en_Espa%C3%B1a_e_Hispanoam%C3%A9rica");

preg_match_all('%<a href="/wiki/.*?_(apellido)" title=".*? (apellido)">(.*?)</a>%i', $html, $result, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($result[0]); $i++) {
    echo $result[1][$i];
}
$html=文件获取内容(“https://es.wikipedia.org/wiki/Anexo:Apellidos_m%C3%A1s_comunes_en_Espa%C3%B1a_e_Hispanoam%C3%A9rica");
预匹配全部('%i',$html,$result,预模式顺序);
对于($i=0;$i

但是不工作,我做错了什么?

这将暂时为您节省:

preg_match_all('/<a.*>(.*)<\/a>/imU', $html, $matches);

请注意,如果“apellido”出现在除“title”或“href”之外的其他
a
属性中,则这可能会给您带来误报。

不要使用regexp来分割HTML,请使用DOM解析器库。您可以连接一个示例解决方案吗?不,我不能,这太费事了。你应该自己研究如何使用
DOMDocument
。我应该使用preg\u match\u好吗?不,你根本不应该使用正则表达式。你应该使用
DOMDocument
。这显示了所有的链接文本,但我只需要标题或HREF上有这个词(apellido)的链接。你应该在问题中更清楚地描述它。我修改了我的答案。
preg_match_all('/<a.*apellido.*".*>(.*)<\/a>/imU', $html, $matches);