1

所以基本上我有一个很大的刺痛(几段长)。

我需要从这个字符串中删除所有没有被任何 HTML 标记包围的文本。

例如,这个字符串:

<h1>This is the title</h1>This is a bit of text with no HTML around it<p>This is within a paragraph tag</p>

应转换为:

<h1>This is the title</h1><p>This is within a paragraph tag</p>

我相信这最好用正则表达式来完成,虽然我对它的语法不是很熟悉。

非常感谢所有帮助。


这就是我最终使用的:

<?php
$string = '<h1>This is the title</h1>This is a bit of text with no HTML around it<p>This is within a paragraph tag</p>';
$pattern = '/(<\/[^>]+>)[^<]*(<[^>]+>)/';
$replacement = '$1$2';
echo preg_replace($pattern, $replacement, $string);
?>
4

1 回答 1

3

您可以使用此正则表达式(<\/[^>]+>)[^<]*(<[^>]+>)并替换为$1$2 现场演示

于 2013-11-13T01:16:07.173 回答