Use preg_replace to replace the contents of a tag

I often need to replace the contents of a tag using php with another variable. This is made easy with the preg_replace() function and a nifty regular expression.

$content = '
Some content to replace
'; echo $content; //output:
Some content to replace
//replace content using preg_replace and regex: $content = preg_replace('/]*>.*?<\/div>/i', '
New Content', $content); echo $content; // new output: //
New Content

http://stackoverflow.com/questions/7139342/preg-replace-how-to-remove-contents-inside-a-tag


Discover more from Devin.org

Subscribe to get the latest posts sent to your email.