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.
我想preg_replace在一个字符串上运行一个 PHP 函数,但我需要它来忽略锚标记内的任何内容。
preg_replace
例如。
string = 'alpha beta delta gamma <a href="somelink.html">alpha beta delta gamma</a>' to match = 'beta delta'
我怎样才能让它只接受第一个实例beta delta而不是第二个实例?
beta delta
您可以否定前瞻结束标记:
preg_replace("/$to_match(?![^<]*<\/)/", 'foo', $string);
此外,您不应该在 html 上使用正则表达式。