Problems with ignored apostrophes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kdidymus
Forum Contributor
Posts: 196
Joined: Tue May 13, 2008 3:37 am

Problems with ignored apostrophes

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Problems with ignored apostrophes

Post 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?
kdidymus
Forum Contributor
Posts: 196
Joined: Tue May 13, 2008 3:37 am

Re: Problems with ignored apostrophes

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Problems with ignored apostrophes

Post 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).
kdidymus
Forum Contributor
Posts: 196
Joined: Tue May 13, 2008 3:37 am

Re: Problems with ignored apostrophes

Post by kdidymus »

Thank you. I'm looking in to this now. Starting with a list of which strings I need to convert.

KD.
Post Reply