Skip to content

Instantly share code, notes, and snippets.

@mig1098
Last active January 28, 2016 21:20
Show Gist options
  • Save mig1098/da479a9ecc8f9a6fe1d1 to your computer and use it in GitHub Desktop.
Save mig1098/da479a9ecc8f9a6fe1d1 to your computer and use it in GitHub Desktop.
$str = '<p>teacher is reading</p>
<h1>parent</h1>
<h2>TEACHER</h2>
<address>teacher is ready</address>
<h3>PARENT</h3>
<strong>parent</strong>';
//for word in string too
//i for insensitive
$resp = preg_replace('/<(p|h1|h2|address)>(.*?)(teacher)(.*?)<\/(p|h1|h2|address)>/i','<$1>$2student$4</$5>',$str);
var_dump($resp);
/*
response:
string '<p>student is reading</p>
<h1>parent</h1>
<h2>student</h2>
<address>student is ready</address>
<h3>PARENT</h3>
<strong>parent</strong>' (length=139)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment