You are here

29 October 2014

Search and replace using regular expression in PHP using preg_replace function. It is very powerful way to search and replace. I suggest using some online regular expression tool to test out your patterns.

Source code viewer
  1. /**
  2.  * In regular expressions parentheses are used to determinate subpatterns.
  3.  * Subpattern results are put to variables such as $1, $2, etc.
  4.  */
  5. echo preg_replace('/<picture(.*?)href=/', '<img$1src=', $str);
Programming Language: PHP