是否有适用于PHP的强大而成熟的HTML解析器?快速浏览PEAR并没有改变任何东西(很多类用于生成HTML,而不是用于消费),Google教会了我很多人已经开始然后放弃了各种解析器项目.
对XML解析器不感兴趣(除非那时可以使用非格式良好的HTML)或者使用正则表达式自己攻击它.
澄清意图:我对HTML内容的过滤不感兴趣,我很有兴趣从HTML文档中提取信息.
我想在链接尚未在链接中的字符串中查找 URL
我当前的代码:
$text = "http://www.google.com is a great website. Visit <a href='http://www.google.com' >http://google.com</a>"
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
if(preg_match($reg_exUrl, $text, $url)) {
$links = preg_replace($reg_exUrl, '<a href="'.$url[0].'" rel="nofollow">'.$url[0].'</a>', $_page['content']['external_links']);
}
Run Code Online (Sandbox Code Playgroud)
问题在于它返回了两次链接(这就是它返回的内容):
<a href="http://www.google.com" rel="nofollow">http://www.google.com</a> is a great website. Visit <a href='<a href="http://www.google.com" rel="nofollow">http://www.google.com</a>' ><a href="http://www.google.com" rel="nofollow">http://www.google.com</a></a>
Run Code Online (Sandbox Code Playgroud) 我想解析xml文件,我发现最好的方法是使用DOMDocument()类到目前为止.
示例xml字符串:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<response>
<resData>
<contact:infData xmlns:contact="http://example.com/contact-1.0">
<contact:status s="value1"/>
<contact:status s="value2"/>
<contact:status s="value3"/>
<contact:status s="value4"/>
</contact:infData>
</resData>
</response>
Run Code Online (Sandbox Code Playgroud)
我使用函数dom2array(bellow)来解析dom,但它只返回1个元素(仅限value4)
<?php
function dom2array($node) {
$res = array();
if($node->nodeType == XML_TEXT_NODE){
$res = $node->nodeValue;
}else{
if($node->hasAttributes()){
$attributes = $node->attributes;
if(!is_null($attributes)){
$res['@attributes'] = array();
foreach ($attributes as $index=>$attr) {
$res['@attributes'][$attr->name] = $attr->value;
}
}
}
if($node->hasChildNodes()){
$children = $node->childNodes;
for($i=0;$i<$children->length;$i++){
$child = $children->item($i);
$res[$child->nodeName] = dom2array($child);
}
}
}
return $res;
}
?>
Run Code Online (Sandbox Code Playgroud)
有没有办法解析所有xml元素并将它们发送到数组?
输出数组:
Array
(
[response] => …Run Code Online (Sandbox Code Playgroud) 假设我有下面的代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title of the document</title>
</head>
<body>
<div id="x">Hello</div>
<p>world</p>
<h1>my name</h1>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我需要提取所有 html 标签并将其放入一个数组中,如下所示:
'0' => '<!DOCTYPE html>',
'1' => '<html>',
'2' => '<head>',
'3' => '<meta charset="UTF-8">',
'4' => '<title>Title of the document</title>',
'5' => '</head>',
'6' => '<body>',
'7' => '<div id="x">Hello</div>',
'8' => '<p>world</p>',
'9' => '<h1>my name</h1>',
....
Run Code Online (Sandbox Code Playgroud)
就我而言,我不需要获取标签内的所有现有内容,对我来说,只捕获每个标签的开头就已经很好了。
我怎样才能做到这一点?
php ×4
domdocument ×2
arrays ×1
html ×1
html-parsing ×1
hyperlink ×1
preg-match ×1
preg-replace ×1
xml ×1