你不应该使用正则表达式,当然HTML解析,但这应该分开出
应该你想要的内容。我对PHP的知识有限,所以这只是说明程序。
$tags =
' <
(?:
/?\w+\s*/?
| \w+\s+ (?:".*?"|\'.*?\'|[^>]*?)+\s*/?
| !(?:DOCTYPE.*?|--.*?--)
)>
';
$scripts =
' <
(?:
(?:script|style) \s*
| (?:script|style) \s+ (?:".*?"|\'.*?\'|[^>]*?)+\s*
)>
.*?
</(?:script|style)\s*>
';
$regex =/($scripts | $tags) | ((?:(?!$tags).)+) /xsg;
替换字符串是组别1 catted您 自动换行函数的返回值(这是传递的内容,组2串) 所以像:更换= \ 1。 textwrap(\ 2)
您可以在textwrap内部决定如何处理内容。
测试在Perl(BTW它非常缓慢,淡化为清楚起见):
use strict;
use warnings;
my $tags =
' <
(?:
/?\w+\s*/?
| \w+\s+ (?:".*?"|\'.*?\'|[^>]*?)+\s*/?
| !(?:DOCTYPE.*?|--.*?--)
)>
';
my $scripts =
' <
(?:
(?:script|style) \s*
| (?:script|style) \s+ (?:".*?"|\'.*?\'|[^>]*?)+\s*
)>
.*?
</(?:script|style)\s*>
';
my $html = join '', <DATA>;
while ($html =~/($scripts | $tags) | ((?:(?!$tags).)+) /xsg) {
if (defined $2 && $2 !~ /^\s+$/) {
print $2,"\n";
}
}
来源
2011-01-13 21:36:59
sln
你将不得不解析HTML。做一个搜索,这里有大约1000个关于这个的帖子。也许你可以使用strip_tags然后换行,并与原始结合... – profitphp 2011-01-13 17:09:02