Tuesday, 15 May 2012

Preg_replace in php - multiple replace -


I have $ string, which includes:

  this example  

and I have these 3 expressions:

  $ pattern = array ('/ aa * /', '/ ii * /'); $ Replacement = array ('<>> $ 0 & lt; / i & gt;', ''); Preg_replace ($ pattern, $ substitution, $ string);  

Where, preg_replace returns:

  th < B & gt; I & lt; / B & gt; Pre & lt; & Lt; B & gt; I & lt; / B & gt; & Gt; A & lt; & Lt; B & gt; I & lt; / B & gt; Mple  

and I need output like this:

  th < B & gt; I & lt; / B & gt; & Lt; I & gt; A & lt; / I & gt; Mple  

Which means, that I want to change only the letter in the original string. Is this possible?

This is the trick in my test

  $ Pattern = array ( '/ ([Az | ^ | \ s]) (aa *) /', '/ ([az | ^ | \ s]) (ii *) /'); $ Substitution = array ('$ 1 ; $ 2 & lt; / i & gt;', '$ 1 & lt; b & gt; $ 2 & lt; / b & gt;');  

No comments:

Post a Comment