Skip to content

Instantly share code, notes, and snippets.

@Cosmologist
Last active December 30, 2015 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cosmologist/7873336 to your computer and use it in GitHub Desktop.
Save Cosmologist/7873336 to your computer and use it in GitHub Desktop.
<?php
function safe_nl2br($string) {
$string = str_replace("\n", "<br />", $string);
foreach (array('pre', 'code') as $tag) {
if(preg_match_all("/$tag>(.*?)<\/$tag/", $string, $match)) {
foreach($match as $a){
foreach($a as $b){
$string = str_replace("<$tag>".$b."</$tag>", "<$tag>".str_replace("<br />", "", $b)."</$tag>", $string);
}
}
}
}
return $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment