Jump to content

remove non-alphanumeric characters except underscore


Recommended Posts

Hi

 

I need to strip all non alphanumeric characters from a string except an underscore.

 

Here's what I have so far which works from the non alphanumeric bit, how do I then ignore underscores?

 

$value = ereg_replace("[^A-Za-z0-9]", "", $value );

 

Any help would be greatly appreciated.

Ok, done that and it removes all characters.  I think i'm getting a certain amount of 'garbage' in my strings, due to them being imported from a tab seperated csv file.  Any thoughts how I can 'clean' the input, or check it is set to the correct encoding?

There are several ways, I would first use iconv to remove undesired encoded characters:

 

$value = iconv("UTF-8", "ISO-8859-1//IGNORE", $value);
$value = preg_replace('~\W*~', '', $value);
echo $value;

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.