Jump to content

Removing special characters


me1000

Recommended Posts

OK im working on a RSS feed and its pulling the feed content from my news database. most of the articles have special characters, like single and double quotes...

Im currently using this code,

[code]<?
$q = preg_replace('/[^0-9a-z]/i','',$news_content);

echo $q; ?>[/code]

but I no longer get spaces!!!!

Any help?



Thanks,
Link to comment
https://forums.phpfreaks.com/topic/31924-removing-special-characters/
Share on other sites

Wow, you could go really indepth with this, or you could not.

The reason you arent getting spaces is because spaces arent 0-9 or a-z :p.  I forgot how to represent space in regexp, but I'm sure someone will know...

Thats also gonna replace things like !@#$%^&*().,/\=+-_~` and any other irregular characters...
Using your code above, an encoded character in an RSS feed like & # 60; (without the spaces) would show up as the number 60 instead of the character '<'.  I'm sure that's not what you intended. 

You can use html_entity_decode() and/or htmlspecialchars_decode() to handle some of the characters.  The others, I would suggest a slight modification to your code where you define the special decimal character and it's replacement.  Use arrays of the 'search for' and 'replace with' characters in the first two places of your preg_replace() statement.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.