怎么我脱衣服 <h1>including this content</h1>
我知道你可以使用条带标签来删除标签,但我希望它们之间的所有内容都消失了.
任何帮助,将不胜感激.
Gum*_*mbo 23
在处理HTML时,您应该使用HTML解析器来正确处理它.您可以使用PHP的DOMDocument并使用DOMXPath查询元素,例如:
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
foreach ($xpath->query('//h1') as $node) {
$node->parentNode->removeChild($node);
}
$html = $doc->saveHTML();
Run Code Online (Sandbox Code Playgroud)
试试这个:
preg_replace('/<h1[^>]*>([\s\S]*?)<\/h1[^>]*>/', '', '<h1>including this content</h1>');
Run Code Online (Sandbox Code Playgroud)
例:
echo preg_replace('/<h1[^>]*>([\s\S]*?)<\/h1[^>]*>/', '', 'Hello<h1>including this content</h1> There !!');
Run Code Online (Sandbox Code Playgroud)
输出:
Hello There
Run Code Online (Sandbox Code Playgroud)
如果要删除所有标记并包含内容:
$yourString = 'Hello <div>Planet</div> Earth. This is some <span class="foo">sample</span> content!';
$regex = '/<[^>]*>[^<]*<[^>]*>/';
echo preg_replace($regex, '', $yourString);
#=> Hello Earth. This is some content!
Run Code Online (Sandbox Code Playgroud)
HTML属性可以包含<或>.所以,如果你的HTML太乱了,这个方法就不行了,你需要一个DOM解析器.
NODE EXPLANATION
--------------------------------------------------------------------------------
< '<'
--------------------------------------------------------------------------------
[^>]* any character except: '>' (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
> '>'
--------------------------------------------------------------------------------
[^<]* any character except: '<' (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
< '<'
--------------------------------------------------------------------------------
[^>]* any character except: '>' (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
> '>'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13208 次 |
| 最近记录: |