removing wierd talking marks

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
Castles
Forum Newbie
Posts: 14
Joined: Tue Nov 04, 2003 10:21 am

removing wierd talking marks

Post by Castles »

Hi, I have written a little program that inserts information into a sql database and then reads from it. When it is finished my client will hopefully copy and paste text into this text box and then the database will automatically update etc.. my problem is, when the user copies from word to the text box (its actually a flash text field) it copies wierd apostraphes... these things ... ''''', they are curly word ones.. when it gets inserted into the database, everything works fine, but when it is read, it oupts "puts it, doesn’t let their" which is meant to be "puts it, doesn't let their" I am using addslashes and strip slashes to read from the database etc.. is there a way to remove these funny apostraphes?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Word is a bug bear of ours with it's curly quotes and elongated dashes :|. To get around this problem I wrote the following function:

Code: Select all

function bbcode_replace_msword_entities($text)
{
	$msword['from'] = array('’', '“', '”', '“', '”', '–', '’', '‘');
	$msword['to'] = array('''', '"', '"', '"', '"', '-', '''', '''');
	$text = str_replace($msword['from'], $msword['to'], $text);
	
	return $text;
} // end func
it's simple but it gets rid of that irritating stuff and I add new entity numbers when I find them.

Mac
Castles
Forum Newbie
Posts: 14
Joined: Tue Nov 04, 2003 10:21 am

Post by Castles »

hmm.. i have tried that but it is still returning the wierd talking marks.. If I view the source it is coming up as <P>‘</P> when it really should be.. <P>'</P>

The code I added tomy page was:

Code: Select all

<?php
//clean up code if it has funny word copy paste stuff
$content = replace_msword_entities($content);


//remove funny word characters
function replace_msword_entities($text) 
{ 
   $msword['from'] = array('&#8217;', '&#8220;', '&#8221;', '“', '”', '&#8211;', '’', '&#8216;', '‘'); 
   $msword['to'] = array('''', '"', '"', '"', '"', '-', '''', '''',''''); 
   $text = str_replace($msword['from'], $msword['to'], $text); 

   return $text; 
}
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

What I tend to do if there's a character that won't shift, is cut it and add it to the end of the $msword['from'] array and what it should be to the end of the $msword['to'] array - sometimes different fonts do things differently.

What do you get in the source for the curly quote if you do:

Code: Select all

$content = replace_msword_entities(htmlentities($content));
BTW, on a side note, you shouldn't need stripslashes() to read from the database, only addslashes() to put things in.

Mac
Castles
Forum Newbie
Posts: 14
Joined: Tue Nov 04, 2003 10:21 am

Post by Castles »

ahh, I see.. thanks for your help.. what i found out was.. because I was getting the content variable from a flash file it was sending a unicode value for the apostraphe..so I wrote a search and replace function that made flash send a non unicode apostraphe and it all worked..

so basically the user will now copy from word into the flash form and then the form will then send it to the php page which will then clean up any yucky code.

The only prob I then had was flash was gerating this '&apos;' for the apostraphe in html code.. and i worked out the php page won't display this like a html page.. yeah.. so just added it to your funtion..

Its all working, thanks for your help... as far as running:

Code: Select all

<?php $content = replace_msword_entities(htmlentities($content)); 

?>
.. it didn't return anything differently..
Post Reply