1
PHP:我該如何替換替換爲「」的HTML標籤?PHP:如何將html標籤替換爲「」?
用下面的代碼:
$str = 'line1<div>line2</div>line3';
echo strip_tags($str);
我得到
line1line2line3
但預期的結果是
line1 line2 line3
感謝
PHP:我該如何替換替換爲「」的HTML標籤?PHP:如何將html標籤替換爲「」?
用下面的代碼:
$str = 'line1<div>line2</div>line3';
echo strip_tags($str);
我得到
line1line2line3
但預期的結果是
line1 line2 line3
感謝
$result = preg_replace('/<.+>/U', ' ', $str);
$rxtags = '
<(?:
(?: (?: (?:script|style) \s* | (?:script|style) \s+ (?:".*?"|\'.*?\'|[^>]*?)+\s*) > .*? </(?:script|style)\s*)
| (?: /?\w+\s*/? | \w+\s+ (?:".*?"|\'.*?\'|[^>]*?)+\s*/? | !(?:DOCTYPE.*?|--.*?--))
)>
';
$html = 'line1<div>line2</div>line3';
$html =~ s/$rxtags/ /xsg;
print $html,"\n";
輸出:line1 line2 line3
替換`」「`,一個單一的空間。如果OP想要保留`<>``,你可能想要使用`+`量詞而不是`*`量詞。 – BoltClock 2011-01-27 14:12:08
根據評論+問題修訂修改了regexp。謝謝:) – Merijn 2011-01-27 14:14:37