Thursday, April 09, 2009

Quote Curling Function

I recently had to add some curly quotes to text on the page. The following function allows you to add Microsoft Word Style Curly Quotes in replacement of standard quotes. Uses some basic regex to accomplish this quickly and easily.


echo curlQuotes('"This" is a "test", a great "test" I think. Don\'t you?');

// Curl Quote Function rob@mechsoftware.com
function curlQuotes($curlme) {
// Adds fancy Microsoft Word Style Quotes to standard text.
$curlme = preg_replace('/(.+)"(.+)"(.+)/Ss',"$1“$2”$3",$curlme);
$curlme = preg_replace('/(.+)"(.+)"/Ss',"$1“$2”",$curlme);
$curlme = preg_replace('/"(.+)"(.+)/Ss',"“$1”$2",$curlme);
$curlme = str_replace("'",'’',$curlme);
return $curlme;
}


Output is:

“This” is a “test”, a great “test” I think. Don’t you?

No comments: