![](/img/trans.png)
[英]How to take content from json using regular expression (regex) in php
[英]How to find content using PHP regular expression
我想在 DIV 中查找内容。 我只想指定 DIV 的 id; rest 在preg_match
下自动调整。 例如:
<div id="midbuttonarea" etc...>some text...</div>
使用 HTML 解析器来解析 HTML 而不是正则表达式,您更有可能获得良好的结果。 用 Regex 解析 HTML 非常困难,结果可能不太可靠。
检查这个问题的公认答案: How do you parse and process HTML/XML in PHP? 有关如何 go 的一些建议。
布伦顿是对的。 无论如何,这里有:
<?php
function findDivInnerHtml($html, $id){
preg_match("/<div .* id=\"{$id}\" .*>(.*)<\\/div>/i", $html, $matches);
return $matches[1];
}
?>
示例用法:
<?php
$html = '<div id="other"> xxx </div> <div id="midbuttonarea" etc...>some text...</div> ';
$innerHTML = findDivInnerHtml($html, 'midbuttonarea');
echo $innerHTML; //outputs "some text..."
?>
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.