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
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...
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.