Created
March 16, 2012 14:40
-
-
Save joshuamilford/2050344 to your computer and use it in GitHub Desktop.
php typography-izer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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, '“' . $matches[1][$key] . '”', $string); | |
} | |
//replace single quotes | |
preg_match_all("/(?<!\\\)'([^']+)'/", $string, $matches); | |
foreach($matches[0] as $key=>$value) | |
{ | |
$string = str_replace($value, '‘' . $matches[1][$key] . '’', $string); | |
} | |
//replace apostrophes | |
$string = preg_replace("/(?<!\\\)'/", '’', $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