2010-08-04 54 views
4

后更换我有一个HTML(sample.html)是这样的:获取内容和处理

<html> 
<head> 
</head> 
<body> 
<div id="content"> 
<!--content--> 

<p>some content</p> 

<!--content--> 
</div> 
</body> 
</html> 

我要如何那是2 HTML注释'<!--content-->'之间的内容部分使用PHP?我想弄明白,做一些处理并把它放回去,所以我必须得到并放下!可能吗?

+0

通过“内容”你的意思是'一些content'或'

一些内容

' 并且评论节点总会被写入'<! - content - >'? – Gordon 2010-08-04 10:05:03

回答

14

esafwan - 你可以使用正则表达式来提取div(特定id)之间的内容。

我之前为这个图片标签做过这个,所以应用了相同的规则。我会查看代码并稍微更新消息。

[更新]试试这个:

<?php 
    function get_tag($attr, $value, $xml) { 

     $attr = preg_quote($attr); 
     $value = preg_quote($value); 

     $tag_regex = '/<div[^>]*'.$attr.'="'.$value.'">(.*?)<\\/div>/si'; 

     preg_match($tag_regex, 
     $xml, 
     $matches); 
     return $matches[1]; 
    } 

    $yourentirehtml = file_get_contents("test.html"); 
    $extract = get_tag('id', 'content', $yourentirehtml); 
    echo $extract; 
?> 

或者更简单地说:

preg_match("/<div[^>]*id=\"content\">(.*?)<\\/div>/si", $text, $match); 
$content = $match[1]; 

吉姆

+0

那么'<! - content - >'中的'id'属性在哪里? – Gordon 2010-08-04 10:24:15

+0

gordon - 拉出的部分是包含在内容(id)div内的内容。沿着与jQuery $('#content')。html()函数 – 2010-08-04 10:32:20

+0

相同的线条,但我如何实际加载html到$ yourentirehtml? – esafwan 2010-08-04 10:36:57

8

如果这是一个简单更换不涉及实际的解析HTML文档中,您可以使用正则表达式,甚至只需使用str_replace即可。但一般来说,it is not a advisable to use Regex for HTML,因为HTML不规则和coming up with reliable patterns can quickly become a nightmare

正确的方法​​是使用一个解析库,它实际上知道如何理解HTML文档。您最好的原生赌注是DOM,但PHP有一些other native XML extensions您可以使用,并且还有一些第三方库,如phpQuery,Zend_Dom,QueryPathFluentDom

如果你使用search function, you will see that this topic has been covered extensively,你应该没有问题找到示例来展示如何解决你的问题。

+0

好点提出重新可靠模式 – 2010-08-04 10:33:51

+1

+1,如果你正在寻找一个适当的XPath匹配节点,它是'(// * | // text())[before-sibling :: comment()='content'and following-sibling :: comment()='content']' – Wrikken 2010-08-04 10:50:09

+0

Thanx ...所有链接帮助我很多,但它没有直接回答我的问题。链接值得阅读,并帮助我在php中获得更多深度! – esafwan 2010-08-04 11:32:28

2
<?php 

    $content=file_get_contents("sample.html"); 
    $comment=explode("<!--content-->",$content); 
    $comment=explode("<!--content-->",$comment[1]); 
    var_dump(strip_tags($comment[0])); 
?> 

检查这一点,它会为

1

你的问题的工作是与嵌套的div 我找到了解决方案here

<?php // File: MatchAllDivMain.php 
// Read html file to be processed into $data variable 
$data = file_get_contents('test.html'); 
// Commented regex to extract contents from <div class="main">contents</div> 
// where "contents" may contain nested <div>s. 
// Regex uses PCRE's recursive (?1) sub expression syntax to recurs group 1 
$pattern_long = '{   # recursive regex to capture contents of "main" DIV 
<div\s+class="main"\s*>    # match the "main" class DIV opening tag 
    (         # capture "main" DIV contents into $1 
    (?:        # non-cap group for nesting * quantifier 
     (?: (?!<div[^>]*>|</div>).)++ # possessively match all non-DIV tag chars 
    |         # or 
     <div[^>]*>(?1)</div>   # recursively match nested <div>xyz</div> 
    )*        # loop however deep as necessary 
)         # end group 1 capture 
</div>        # match the "main" class DIV closing tag 
}six'; // single-line (dot matches all), ignore case and free spacing modes ON 

// short version of same regex 
$pattern_short = '{<div\s+class="main"\s*>((?:(?:(?!<div[^>]*>|</div>).)++|<div[^>]*>(? 1)</div>)*)</div>}si'; 

$matchcount = preg_match_all($pattern_long, $data, $matches); 
// $matchcount = preg_match_all($pattern_short, $data, $matches); 
echo("<pre>\n"); 
if ($matchcount > 0) { 
    echo("$matchcount matches found.\n"); 
// print_r($matches); 
    for($i = 0; $i < $matchcount; $i++) { 
     echo("\nMatch #" . ($i + 1) . ":\n"); 
     echo($matches[1][$i]); // print 1st capture group for match number i 
    } 
} else { 
    echo('No matches'); 
} 
echo("\n</pre>"); 
?>