获取内容和处理
后更换我有一个HTML(sample.html)是这样的:获取内容和处理
<html>
<head>
</head>
<body>
<div id="content">
<!--content-->
<p>some content</p>
<!--content-->
</div>
</body>
</html>
我要如何那是2 HTML注释'<!--content-->'
之间的内容部分使用PHP?我想弄明白,做一些处理并把它放回去,所以我必须得到并放下!可能吗?
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];
吉姆
看一看这里,这意味着你可以加载HTML代码示例文件转换为SimpleXML http://blog.charlvn.com/2009/03/html-in-php-simplexml.html
然后,您可以将其视为正常的SimpleXML对象。
编辑:这个,如果你想在标签中的内容只会工作(如之间<DIV>和</DIV >)
如果这是一个简单更换不涉及实际的解析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,QueryPath和FluentDom。
如果你使用search function, you will see that this topic has been covered extensively,你应该没有问题找到示例来展示如何解决你的问题。
<?php
$content=file_get_contents("sample.html");
$comment=explode("<!--content-->",$content);
$comment=explode("<!--content-->",$comment[1]);
var_dump(strip_tags($comment[0]));
?>
检查这一点,它会为
你的问题的工作是与嵌套的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>");
?>
通过“内容”你的意思是'一些content'或'
一些内容
' 并且评论节点总会被写入''? – Gordon 2010-08-04 10:05:03