php preg_replace - 如何删除标签内的内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7139342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
preg_replace - How to remove contents inside a tag?
提问by Kavin
Say I have this.
说我有这个。
$string = "<div class=\"name\">anyting</div>1234<div class=\"name\">anyting</div>abcd";
$regex = "#([<]div)(.*)([<]/div[>])#";
echo preg_replace($regex,'',$string);
The output is abcd
输出是 abcd
But I want 1234abcd
但我想要 1234abcd
How do I do it?
我该怎么做?
回答by sg3s
Like this:
像这样:
preg_replace('/(<div[^>]*>)(.*?)(<\/div>)/i', '', $string);
If you want to remove the divs too:
如果您也想删除 div:
preg_replace('/<div[^>]*>.*?<\/div>/i', '', $string);
To replace only the content in the divs with class name and not other classes:
只用类名而不是其他类替换 div 中的内容:
preg_replace('/(<div.*?class="name"[^>]*>)(.*?)(<\/div>)/i', '', $string);
回答by CONvid19
$string = "<div class=\"name\">anything</div>1234<div class=\"name\">anything</div>abcd";
echo preg_replace('%<div.*?</div>%i', '', $string); // echo's 1234abcd
Live example:
现场示例:
回答by genesis
add ?
, it will find FIRST occurence
添加?
,它将找到第一次出现
preg_replace('~<div .*?>(.*?)</div>~','', $string);
回答by Felix Kling
This might be a simple example, but if you have a more complex one, use an HTML/XML parser. For example with DOMDocument
:
这可能是一个简单的示例,但如果您有一个更复杂的示例,请使用 HTML/XML 解析器。例如DOMDocument
:
$doc = DOMDocument::loadHTML($string);
$xpath = new DOMXPath($doc);
$query = "//body/text()";
$nodes = $xpath->query($query);
$text = "";
foreach($nodes as $node) {
$text .= $node->wholeText;
}
Which query you have to use or whether you have to process the DOM tree in some other way, depends on the particular content you have.
您必须使用哪个查询或是否必须以其他方式处理 DOM 树,取决于您拥有的特定内容。