Skip to content

Instantly share code, notes, and snippets.

@joshuamilford
Created March 16, 2012 14:40
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 joshuamilford/2050344 to your computer and use it in GitHub Desktop.
Save joshuamilford/2050344 to your computer and use it in GitHub Desktop.
php typography-izer
//escape everything in tags
preg_match_all('/(\<[^\>]+\>)/', $string, $tags);
foreach($tags[0] as $key=>$value)
{
$string = str_replace($value, str_replace(array('"', "'"), array('\"', "\'"), $value), $string);
}
//replace double quotes
preg_match_all('/(?<!\\\)"([^"]+)"/', $string, $matches);
foreach($matches[0] as $key=>$value)
{
$string = str_replace($value, '&ldquo;' . $matches[1][$key] . '&rdquo;', $string);
}
//replace single quotes
preg_match_all("/(?<!\\\)'([^']+)'/", $string, $matches);
foreach($matches[0] as $key=>$value)
{
$string = str_replace($value, '&lsquo;' . $matches[1][$key] . '&rsquo;', $string);
}
//replace apostrophes
$string = preg_replace("/(?<!\\\)'/", '&#8217;', $string);
//clean up the stuff we escaped
$string = preg_replace('/\\\"/', '"', $string);
$string = preg_replace("/\\\'/", "'", $string);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment