Forum Moderators: coopster

Message Too Old, No Replies

Little help with a reg. expression

Replace upquotes by nicer ones...

         

floz

4:55 pm on Sep 26, 2005 (gmt 0)

10+ Year Member



Hi there!

I need some help with a regular expression which does the following thing:

search pairs of double quotes ("bla") and replace them by nice, typograpic ones (“bla”).

the text the preg will have to work on is nothing fancy / evil (i.e. doesn't contain html one would have to worry about). just the pure matching of quotes and replacing them by nice typographic ones...

thanks for your help!

Florian

coopster

8:54 pm on Sep 26, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'm assuming you mean to print out in HTML?
$before = 'This is my "double quotation mark" quote'; 
print preg_replace('/"([^"]*)"/', "“$1”", $before);

floz

9:02 pm on Sep 26, 2005 (gmt 0)

10+ Year Member



yes, thanks a million. exactly what i wanted : )

now, i go back and say the text-to-be-modified *may* contain html, so all double quotes inside angles (<...>) must stay untouched........

how to do that?

thx again for your help!

coopster

11:37 pm on Sep 26, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Oh sure, now you want to go further ;)

You'll have to modify your logic and/or the regular expression to check for the possibility of HTML form elements and their quoted attributes. One trick that I could think of off-hand would be to search for the possibility of an equal sign right before the first double quotation mark perhaps.

floz

7:25 am on Sep 27, 2005 (gmt 0)

10+ Year Member



yapp, that would be an idea. or, as i suggested, if it is <enclosed in angles> ...

sorry for becoming more greedy ;-)