preg_replace在单词周围添加以某个单词开头的链接


preg_replace add links around words starting with some-part-of-a-word

我在php中有这个preg_replace,它几乎正确地将以"漏洞利用"开头的每个单词替换为链接:

preg_replace('#['b]?(exploit([^ ]*))['b]?#', '<a>$1</a>', 'My exploits are exploitable.');

我得到这个:

My <a>exploits</a> are <a>exploitable.</a>

这是错误的一半,句号不应该链接在第二个单词上。我知道我需要把上面的部分[^]替换成类似[^''b]的东西,但它不起作用。

我知道我总是可以做,例如[^.],但它只适用于以空格和句号结尾的单词,而不是逗号。

'b(exploit[a-zA-Z]*)应该能做到这一点。

旁白:这个网站非常方便试用preg_replace:http://www.fullonrobotchubby.co.uk/random/preg_tester/preg_tester.php

'bexploit('w*)'b-更简单。。。。

玩得开心

regexrDemo

编辑

如果你只想要字母,请评论:

/'bexploit([a-z]*)'b/i

regexrDemo-only字母

如果你想处理unicode,你可以做:

preg_replace('#'b(exploit'pL*)#u', '<a>$1</a>', 'My exploits are exploitable.');

其中'pL代表任何字母。