Thursday, 15 August 2013

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

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

I've got this preg_replace in php which almost correctly replaces every
word starting with 'exploit' into links:
preg_replace('#[\b]?(exploit([^ ]*))[\b]?#', '<a>$1</a>', 'My exploits are
exploitable.');
I get this:
My <a>exploits</a> are <a>exploitable.</a>
Which is half wrong, the fullstop should not be linked on the second word.
I know I need to replace the above part [^ ] to something like [^\b] but
it doesn't work.
I know I can always do i.e [^ .] but it would only work on words ending
with space and fullstops, not commas for example.

No comments:

Post a Comment