tags:

views:

673

answers:

2

Say you have a string, but you don't know what it contains. And you want to replace all occurences of a particular word or part of a word with a formatted version of the same word. For example, I have a string that contains "lorem ipsum" and i want to replace the entire word that contains "lo" with "lorem can" so that the end result would be "lorem can ipsum" but if I put the string "loreal ipsum" through the same function, the result would now be "loreal can ipsum". Thanks

A: 

$str = preg_replace('/lo(\w*)/', 'lo$1 can', $str);

This replaces "lo" plus any word characters with "lo" + the other characters + " can"

It will also replace "lo" with "lo can" - if you don't want this, change \w* to \w+

Greg
+4  A: 
$str = preg_replace('/(\blo[a-z]+\b)/', '$1 can', $str);

The problem with RoBorg's answer are:

  1. \w matches digits and underscores, which aren't really word characters in human language, so it would match 'lo_fi' or '__lo__'.
  2. it would also match words that don't begin with lo, such as 'slorem', or even words that end in lo such as 'allo'.

\b makes sure lo follows a word-break (zero-width) and [a-z]+ ensures that at least one alphabetical character follows 'lo'.

Edit: I see the text of the question says "contains" rather than "begins with", in which case, the first \b may be omitted.

Edit 2: Note that neither of these solutions are international-safe. I haven't tested this, but PHP's RegEx is pretty capable so I'm guessing it'll work:

$str = preg_replace('/(\blo[a-z\p{L}]+\b)/', '$1 can', $str);
eyelidlessness