Bizarre characters on page - how to stop??

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
scottrichardson
Forum Newbie
Posts: 9
Joined: Wed May 21, 2008 12:14 am

Bizarre characters on page - how to stop??

Post by scottrichardson »

Hi all,

On ny site, my users can submit news articles and other content via forms which insert content into mysql.

When inserting text containing certain characters, it's replacing them with strange groups of characters.

Take a look, see the bits in red:

"Ibiza - the birthplace of modern dance music. 20 years on and the island remains an important catalyst for todayâ•?s hottest dance talents. This year, OceanLab, the project featuring Above & Beyond and singer/songwriter Justine Suissa spent several weeks holed up in a villa there finishing their debut album, â•?Sirens Of The Seaâ•?."

Now i know this has to do with text encoding. But I'm not sure how to overcome this.

Every time people add content to my site, I have to edit it.

I did find a function online somewhere, but it appears not to be working as it should.... or at all??

here is the function:

Code: Select all

function smarties($string) 
{ 
       /**
     *  ‘  8216  curly left single quote
     *  ’  8217  apostrophe, curly right single quote
     *  “  8220  curly left double quote
     *  ”  8221  curly right double quote
     *  —  8212  em dash
     *  –  8211  en dash
     *  …  8230  ellipsis
     */
    $search = array(
                    '&',
                    '"',
                    chr(212),
                    chr(213),
                    chr(210),
                    chr(211),
                    chr(209),
                    chr(208),
                    chr(201),
                    chr(145),
                    chr(146),
                    chr(147),
                    chr(148),
                    chr(151),
                    chr(150),
                    chr(133)
                    );
    $replace = array(
                    '&',
                    '"',
                    '‘',
                    '’',
                    '“',
                    '”',
                    '–',
                    '—',
                    '…',
                    '‘',
                    '’',
                    '“',
                    '”',
                    '–',
                    '—',
                    '…'
                    );
     
    return str_replace($search, $replace, $string); 
}
I'm sure someone has come across this before. Any suggestions would be greatly appreciated.

Kind regards,

Scott
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Bizarre characters on page - how to stop??

Post by superdezign »

Websites have a type of text encoding and databases have a type of text encoding. For example, this forum uses UTF-8. UTF-8 would be my personal recommendation, because ISO-8859-1 tends to have problems similar to this. Make sure that your website and your database agree in terms of text encoding.
scottrichardson
Forum Newbie
Posts: 9
Joined: Wed May 21, 2008 12:14 am

Re: Bizarre characters on page - how to stop??

Post by scottrichardson »

Will do.

Actually, by switching to UTF- (I was in ISO) for my web pages, it has appeared to fix all of the characters except for a few, which now appear as diamonds with question marks in them. HOWEVER, I have not altered my database yet. It's currently in latin. I will switch the tables to UTF and see how that goes.

Thanks

Scott
adguru
Forum Newbie
Posts: 3
Joined: Sun Jun 15, 2008 12:24 pm
Location: Canada

Re: Bizarre characters on page - how to stop??

Post by adguru »

Create a filter array and use it everytime. For example,

$x[0][0]="splchar"
$x[0][1]="text equivalent"

I hope this helps, most of the forums use this technique.
scottrichardson
Forum Newbie
Posts: 9
Joined: Wed May 21, 2008 12:14 am

Re: Bizarre characters on page - how to stop??

Post by scottrichardson »

Hi adguru, please excuse my lofi knowledge of arrays.. could you show me how I would fully implement that?

What the full code would be?

Scott
Post Reply