Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
所以我有一个看起来像这样的字符串<span>hey</span>/<span>bye</span>/<span>why</span>/。我将如何用替换preg_replace所有非 HTML-tag ?/::
<span>hey</span>/<span>bye</span>/<span>why</span>/
preg_replace
/
::
我会考虑为此使用负面的lookbehind:
$pattern = '#(?<!<)/#'; $replacement = '::'; $result = preg_replace($pattern, $replacement, $input);
这样做的要点是它只会替换/前面的字符不是<
<
当然,您可能需要更进一步,确保它没有以下字符>,例如自闭合标签的情况。在这种情况下,您也可以添加负前瞻,使模式变为:
>
$pattern = '#(?<!<)/(?!>)#';