如何使用PHP获取HTML标记中属性的值
我有一根像这样的绳子如何使用PHP获取HTML标记中属性的值,php,regex,domdocument,Php,Regex,Domdocument,我有一根像这样的绳子 $str = '<a href="http://www.example.com/example_link">This is the Example Link</a>'; $str=''; 我想获得href值(http://www.example.com/example_link)和上面php字符串中的文本值(这是示例链接) 如何从字符串中获取这些值,以任何有效的方式。我需要使用PHP DOM还是正则表达式 注意:一个字符串中可能出现多个锚点标记。
$str = '<a href="http://www.example.com/example_link">This is the Example Link</a>';
$str='';
我想获得href值(http://www.example.com/example_link)
和上面php字符串中的文本值(这是示例链接)
如何从字符串中获取这些值,以任何有效的方式。我需要使用PHP DOM还是正则表达式
注意:一个字符串中可能出现多个锚点标记。如果您确定字符串的xml格式正确,则可以使用 试试这个
<?php
$str = '<a href="http://www.example.com/example_link">This is the Example Link</a>';
$xml = simplexml_load_string($str);
echo "<pre>";
print_r($xml);
?>
它将返回一个对象,您可以访问任何您想要的内容
首先找到链接 ` //正则表达式过滤器
$reg|u exUrl=“/(http | https | ftp | ftps)\://[a-zA-Z0-9-.]+[a-zA-Z]{2,3}(/\S*)?/”
//要筛选URL的文本
$text=''
//检查文本中是否有url
如果(preg_match($reg_exUrl,$text,$url)){
$link=$url[0];
}否则{
$link='发生了一些问题';
} 现在查找锚定之间的文本
$link\u text=preg\u replace('//i','$text)
$link_text=preg_replace('//i','$link_text)
?>
您需要正则表达式和
preg\u match\u all
。模式类似于/