Page 1 of 1

Problems with ignored apostrophes

Posted: Sat Feb 28, 2009 3:09 am
by kdidymus
I'm not a novice in PHP but I'm still going to need an idiots guide to get around a problem I'm experiencing.

I use PHP to download data from a MySQL database. One of the pages I've designed downloads a photograph and description from the database and displays it.

However, some of the photograph descriptions on the MySQL dB have apostrophes in.

i.e.
Phyllis' grave at Nanstallon
Esmé photographed in the 1950's
Here is a snippet of my code:

Code: Select all

<a href='$photolink$photo2urn.gif' rel='lightbox[$mainurn]' title='$photo2description' $reltext>&nbsp;2&nbsp;</a>
Any idea how I can get the apostrophe's to display?

Thanks very much in advance.

KD.

Re: Problems with ignored apostrophes

Posted: Sat Feb 28, 2009 3:27 am
by requinix
Run the text through htmlentities first making sure to use the ENT_QUOTES flag.

Wondering why this problem happens?

Code: Select all

title='Phyllis' grave at Nanstallon'
See what happens?

Re: Problems with ignored apostrophes

Posted: Sun Mar 01, 2009 9:27 am
by kdidymus
Absolutely. The apostrophe in the data downloaded is being treated by PHP as the closing apostrophe.

My problem is, the nature of my site means that I download a vast amount of data and various strings are created throughout the page.

Is there a way to "blanket" convert any and all data as it's downloaded? Or do I need to specify htmlentities for each string?

I've looked on the net a fair bit but I'm getting nowhere fast!

KD.

Re: Problems with ignored apostrophes

Posted: Sun Mar 01, 2009 9:39 am
by requinix
You should do it for every string, but there are ways to make your job easier. It all depends on how these strings are stored in PHP.

For example, if you have an array of strings then you can use array_map with "htmlentities" as the callback. If the array is more complex then you can make a function that takes one of those array elements and htmlentities the string (wherever it is).

Re: Problems with ignored apostrophes

Posted: Sun Mar 01, 2009 11:27 pm
by kdidymus
Thank you. I'm looking in to this now. Starting with a list of which strings I need to convert.

KD.