Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PHP获取HTML标记中属性的值_Php_Regex_Domdocument - Fatal编程技术网

如何使用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
。模式类似于
/