Preg_Replace all strings between two tags

What?
For those of you who use Preg_Replace. Preg_replace is a function that uses regular expressions to search and replace a string.

Why?
Because my understanding with regular expressions is shady and varies from language to language, I've written this article as a quick reference point.

How?
Well you use this in your PHP scripts, the examples below are meant to help but feel free to comment if they do not:

Surround string (apostrophes) with red styling
Convert: this='a string'  »  this='a string'
copyraw
$string_formatted = preg_replace ( '/\'([^\']+)\'/', '<span style="color:red">$0</span>', $string_original );
  1.  $string_formatted = preg_replace ( '/\'([^\']+)\'/', '<span style="color:red">$0</span>', $string_original )

Surround string (apostrophes) with red styling but ignore escaped apostrophes:
Convert: this='joe\'s string' » this='joe\'s string'
copyraw
$string_formatted = str_replace('\\\'', '\\\&#39;', $string_formatted);  // bit of a copout I know
$string_formatted = preg_replace ( '/\'([^\']+)\'/', '<span style="color:red">$0</span>', $string_formatted );
  1.  $string_formatted = str_replace('&#92;&#92;\'', '&#92;&#92;&#92;&#39;', $string_formatted);  // bit of a copout I know 
  2.  $string_formatted = preg_replace ( '/\'([^\']+)\'/', '<span style="color:red">$0</span>', $string_formatted )


Surround string (apostrophes) with red styling and evaluate:
Convert: this='some <html> string' » this='some string'
copyraw
$string_formatted = preg_replace ( '/\'([^\']+)\'/e', 'stripslashes(strip_tags("$0"))', $string_formatted );  // really important to suffix the expression with 'e'
$string_formatted = preg_replace ( '/\'([^\']+)\'/', '<span style="color:red">$0</span>', $string_formatted );
  1.  $string_formatted = preg_replace ( '/\'([^\']+)\'/e', 'stripslashes(strip_tags("$0"))', $string_formatted );  // really important to suffix the expression with 'e' 
  2.  $string_formatted = preg_replace ( '/\'([^\']+)\'/', '<span style="color:red">$0</span>', $string_formatted )


Surround string (backticks) with darkred styling
Convert: `mysql_reserved_word`=my_alias  »  `mysql_reserved_word`=my_alias
copyraw
$string_formatted = preg_replace('/<pre>(.*)<\/pre>/eis', "'<span style="color:red">'.'$1'.'</span>'", $string_original);
  1.  $string_formatted = preg_replace('/<pre>(.*)<\/pre>/eis', "'<span style="color:red">'.'$1'.'</span>'", $string_original)

Replace contents between two tags (<pre></pre>)
Convert: my outside string <pre>is now inside</pre>  »  my outside string is now inside
copyraw
$string_formatted = preg_replace('/(<pre>)(.*)(<\/pre>)/eis', "'<span style="color:red">' . '$1' . '$2' . '$3' . '</span>'", $string_original);
  1.  $string_formatted = preg_replace('/(<pre>)(.*)(<\/pre>)/eis', "'<span style="color:red">' . '$1' . '$2' . '$3' . '</span>'", $string_original)

Replace contents between two tags (<pre></pre>) INCLUSIVE
Convert: my outside string <pre>is now inside</pre>  »  my outside string <pre>is now inside</pre>
copyraw
$startPoint = '<!-- MY START STRING -->';
$endPoint = '<!-- MY END STRING -->';
$string_formatted = preg_replace('#('.preg_quote($startPoint).')(.*)('.preg_quote($endPoint).')#si', '$0', $string_original);
  1.  $startPoint = '<!-- MY START STRING -->'; 
  2.  $endPoint = '<!-- MY END STRING -->'; 
  3.  $string_formatted = preg_replace('#('.preg_quote($startPoint).')(.*)('.preg_quote($endPoint).')#si', '$0', $string_original)

Replace contents between two strings
Convert: <!-- MY START STRING -->Hello World!<!-- MY END STRING -->  »  Hello World!
copyraw
$string_formatted = preg_replace( '/@[a-z]+/i','<span style="color:red;">$0</span>', $string_original);

-- METHOD #2: equivalent using preg_match_all 
$matches = null;
preg_match_all('/(?!\b)(@\w+\b)/',$string_original,$matches);
foreach ($matches as $match) {
	foreach ($match as $mystring) {
		$string_formatted = str_replace( $mystring, '<span style="color:red;">'.$mystring.'</span>', $string_formatted); 
	}
}
  1.  $string_formatted = preg_replace( '/@[a-z]+/i','<span style="color:red;">$0</span>', $string_original)
  2.   
  3.  -- METHOD #2: equivalent using preg_match_all 
  4.  $matches = null
  5.  preg_match_all('/(?!\b)(@\w+\b)/',$string_original,$matches)
  6.  foreach ($matches as $match) { 
  7.      foreach ($match as $mystring) { 
  8.          $string_formatted = str_replace( $mystring, '<span style="color:red;">'.$mystring.'</span>', $string_formatted)
  9.      } 
  10.  } 


Replace all @words
Convert: Hello @World » Hello @World
copyraw
$string_formatted = preg_replace( '/\$[a-z_]+/i','<span style="color:red;">$0</span>', $string_original);
  1.  $string_formatted = preg_replace( '/\$[a-z_]+/i','<span style="color:red;">$0</span>', $string_original)


Replace all $wordpart1_wordpart2 (allow underscores)
Convert: Hello $New_World » Hello $New_World
copyraw
$string_formatted = preg_replace( '/[a-z]+\(/i','<span style="color:red;">$0</span>', $string_original);
  1.  $string_formatted = preg_replace( '/[a-z]+\(/i','<span style="color:red;">$0</span>', $string_original)


Replace all words with suffixed character
Convert: Hello( World ) » Hello( World )
copyraw
$string_formatted = preg_replace( '/[a-z_ -]+\&nbsp;\(/i','<span style="color:red;">$0</span>', $string_original);
  1.  $string_formatted = preg_replace( '/[a-z_ -]+\&nbsp;\(/i','<span style="color:red;">$0</span>', $string_original)


Replace all words with suffixed character and html entity
Convert: Hello&nbsp;( World ) » Hello&nbsp;( World )
$string_formatted = preg_replace( '/[a-z_ -]+\&nbsp;\(/i','<span style="color:red;">$0</span>', $string_original);


Anymore? Please leave a comment if I've missed some useful ones out.
Category: Personal Home Page :: Article: 484

Add comment

Your rating:

Submit

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Please publish modules in offcanvas position.