开发者

PHP Pretty Quotes

开发者 https://www.devze.com 2023-01-10 03:18 出处:网络
What\'s the best way to get PHP to convert regular quotes to pretty quotes. i.e., convert - straight quotes:

What's the best way to get PHP to convert regular quotes to pretty quotes.

i.e., convert -

straight quotes:

Poe's Great Aunt Sally said Poe said, "Once upon a midday dreary."

converted to curly "pretty quotes":

Poe’s Great Aunt Sally said Poe said, “Once upon a midda开发者_开发百科y dreary.”


Try this out:

function convert_quotes($string) {
    $string = " " . $string . " "; //add spaces to beginning and end of string to catch strings that begin and/or end with quotes
    $search = array(" '", //use spaces to determine which direction a quote show curl.
                     "' ",
                     "'",
                     ' "',
                     '" '
    );
    $replace = array("‘",
                    "’",
                    "’",
                    "“", 
                    "”"
    );

    return trim(str_replace($search, $replace, $string)); //replace quotes and trim spaces added at beginning of function
}


There's already a plugin called "SmartyPants," originally created by John Gruber of Daring Fireball fame. It was originally created for MoveableType

But others have already created PHP versions that you can use. Check out this one:

http://michelf.com/projects/php-smartypants/

Or you can just run a Google search for "SmartyPants PHP" and you'll find more options.

Also for reference, people also refer to these as "smart quotes."

0

精彩评论

暂无评论...
验证码 换一张
取 消