PHP and Word’s special characters

Sick of users copying and pasting Word content into your web forms?  We all know you end up with all their special characters ending up as ? in your text.  This recently happened to me and I had to fix it, see these were college essays on the undergraduate application.

This is what I used to convert them to html friendly characters :

function convert_raw_utf8_smart_quotes($string)
{
    $search = array(chr(0xe2) . chr(0x80) . chr(0x98),
	chr(0xe2) . chr(0x80) . chr(0x99),
	chr(0xe2) . chr(0x80) . chr(0x9c),
	chr(0xe2) . chr(0x80) . chr(0x9d),
	chr(0xe2) . chr(0x80) . chr(0x93),
	chr(0xe2) . chr(0x80) . chr(0x94));

    $replace = array('‘',
	'’',
	'“',
	'”',
	'–',
	'—');
    return str_replace($search, $replace, $string);
}

 

This worked great until I had to pump out a PDF file for admissions using this content, and the above & codes don’t look so pretty. So I played a little music chairs with the code and ended up calling this when I had to display the code in the PDF file –

function convert_smart_quotes_for_PDF ($string) 
{ 
    #this reverses the process for the PDF rendering
    $search = array('‘',
	'’',
	'“',
	'”',
	'–',
	'—');

    $replace= array("'", 
      "'", 
      '"', 
      '"',
      '-',
      '-'); 

    return str_replace($search, $replace, $string); 
}

 

Enjoy

Code it right
theCodeDog
theCodeDog

posted in PHP by Jonathan

Follow comments via the RSS Feed | Leave a comment | Trackback URL

Leave Your Comment

 
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org