I'm trying to make a web form where I can type text into an administrative
textarea and then get it displayed on a public page. Simple, right? Only I
want to be able to type in straight quotes (Ray "Boom-Boom" Mancini) and
have it appear on the page as curved quotes (Ray “Boom-Boom” Mancini).
I want similar effects for single quotes and apostrophes.
I tried achieving this with regular expressions and had some success, but I
have never made it work completely. This might have been a post about
regular expressions, but instead I'll ask: Does anyone know an easy way to
achieve the results I'm describing? All I seem to find when Googling are
ways to change curly quotes into straight quotes instead of the other way
around.
straight quotes to curly quotes in web form
Moderator: General Moderators
Re: straight quotes to curly quotes in web form
That looks like an okay solution, but what about the following:
Obviously the first " is in error.
(No, I don't have a solution for this...but..)
Ideally it should look for quotes enclosing words or sentences.
Code: Select all
This is some" text that should be quoted: "hello, there"(No, I don't have a solution for this...but..)
Ideally it should look for quotes enclosing words or sentences.
Re: straight quotes to curly quotes in web form
-_-McInfo wrote:It is obvious to you that the first quote is out of place because your mind assigns meaning to the words.
Thank you, yes, that was my point.
When working with human-readable text this directly, it's the coder's job to make the output as readable as possible. Therefore, your job is to make the code understand as much as it can how quotes are related to words and sentences. The english (and most other) languages are fairly convoluted. Therefore the code will also come out fairly convoluted.
In other words...
Code: Select all
preg_replace('/"(\w+?)"/','“$1”',$text); //simple words
/"\s([A-Z][a-z \-,]+?[\.\?\!])"/ //a basic sentence (blah blah develop further)I hope I'm making myself a little clearer.
Re: straight quotes to curly quotes in web form
Btw, I have since solved the problem with the following regexes:
$patterns=array('/(\A|\s)\\\"(\S)/', '/(\S)\\\"(\s|\Z)/', '/(\A|\s)\\\\\'(\S)/', '/(\S)\\\\\'(\s|\Z)/', "/(\S)\\\\\'(\S)/");
$replacements=array("$1“$2", "$1”$2", "$1‘$2", "$1’$2", "$1’$2");
return preg_replace($patterns, $replacements, $s);
where the elements in $patterns match an opening double quote, a closing double quote, an opening single quote, a closing single quote, and an apostrophe, respectively.
I'm still not totally sure why I need five backslashes in patterns 3-5 to match a literal backslash followed by a literal single quote (\'). I would have thought three would do it: one escaped backslash (\\) to match the literal backslash and one escaped single quote to match the literal single quote (\') for a final pattern of \\\'. But that didn't work. Apparently you have to escape backslashes in PHP strings *again* to make them really and truly literal, meaning each of the backslahses in the escaped backslash pattern must be doubled again. Is that so that what follows the backslash won't be interpreted as a variable? Does anyone have any insight into this?
$patterns=array('/(\A|\s)\\\"(\S)/', '/(\S)\\\"(\s|\Z)/', '/(\A|\s)\\\\\'(\S)/', '/(\S)\\\\\'(\s|\Z)/', "/(\S)\\\\\'(\S)/");
$replacements=array("$1“$2", "$1”$2", "$1‘$2", "$1’$2", "$1’$2");
return preg_replace($patterns, $replacements, $s);
where the elements in $patterns match an opening double quote, a closing double quote, an opening single quote, a closing single quote, and an apostrophe, respectively.
I'm still not totally sure why I need five backslashes in patterns 3-5 to match a literal backslash followed by a literal single quote (\'). I would have thought three would do it: one escaped backslash (\\) to match the literal backslash and one escaped single quote to match the literal single quote (\') for a final pattern of \\\'. But that didn't work. Apparently you have to escape backslashes in PHP strings *again* to make them really and truly literal, meaning each of the backslahses in the escaped backslash pattern must be doubled again. Is that so that what follows the backslash won't be interpreted as a variable? Does anyone have any insight into this?