regex - How do I get a particular word from a string in PHP? -
say have string, don't know contains. , want replace occurences of particular word or part of word formatted version of same word. example, have string contains "lorem ipsum" , want replace entire word contains "lo" "lorem can" end result "lorem can ipsum" if put string "loreal ipsum" through same function, result "loreal can ipsum". thanks
$str = preg_replace('/lo(\w*)/', 'lo$1 can', $str);
this replaces "lo" plus word characters "lo" + other characters + " can"
it replace "lo" "lo can" - if don't want this, change \w*
\w+
Comments
Post a Comment