Forum Moderators: coopster

Message Too Old, No Replies

Converting quotation marks in a text file to

         

PopsRick

4:33 pm on Oct 28, 2008 (gmt 0)

10+ Year Member



I have a text file that has quotation marks in it and I need to convert those to " for HTML output. Here is what I am using and it is not working.

$newLine = preg_replace("/\"/",""",$line);

Any ideas? Thanks!

Receptional Andy

4:45 pm on Oct 28, 2008 (gmt 0)



Welcome to WebmasterWorld, PopsRick :)

PHP has a built in function for this: htmlentities [php.net].

The PHP manual [php.net] is very handy too ;)

[edited by: Receptional_Andy at 4:46 pm (utc) on Oct. 28, 2008]

PopsRick

4:54 pm on Oct 28, 2008 (gmt 0)

10+ Year Member



Thanks Andy, I had tried this earlier but I tried it again just to be sure, both with and without ENT_QUOTES.

$newLine = htmlentities($line,ENT_QUOTES);
echo $newLine."<br />\n";

Still not converting the quotation marks. btw, IE is displaying these as small squares or question marks (tried it on two machines).

Receptional Andy

4:55 pm on Oct 28, 2008 (gmt 0)



Are these standard quotes, or "curly quotes" of the kind used in MS office programs?

PopsRick

5:02 pm on Oct 28, 2008 (gmt 0)

10+ Year Member



Good question they look like this ” . You may not be able to see, not sure how it will come across, but they look like italicized quotes. The file is a rich text file btw.

acemaster

5:17 pm on Oct 28, 2008 (gmt 0)

10+ Year Member



Those look like curly quotes to me. I would suggest htmlspecialchars [php.net] as it is faster than entities and does not try to convert awkward characters into entities.

PopsRick

7:03 pm on Oct 28, 2008 (gmt 0)

10+ Year Member



Ok, got it.
htmlspecialchars and htmlentities did not work.

What worked is this, I copied the "curly quotes" (thanks Andy) from the original document and used them in the str_replace functions below.

$newLine=str_replace('“','&quot;',$line);
$newLine=str_replace('”','&quot;',$line);

Thanks for all your help folks!

mooger35

7:17 pm on Oct 28, 2008 (gmt 0)

10+ Year Member



if you are going to use str_replace you should edit it to this:

$newLine=str_replace('“','&quot;',$line);
$newLine=str_replace('”','&quot;',$newLine);

Receptional Andy

7:31 pm on Oct 28, 2008 (gmt 0)



str_replace also accepts arrays, so you could also keep all the characters you wanted to replace with standard quotes there (this is especially useful if you think there might be additional characters to replace in future):

[code][/code]$curlyQuotes=array('“','”');
$newLine=str_replace($curlyQuotes,'&quot;',$line);

PopsRick

9:08 pm on Oct 28, 2008 (gmt 0)

10+ Year Member



Thanks mooger, I stand corrected. :)

Thanks Andy, I didn't know you could do that. :)