Nodral Posted March 29, 2012 Share Posted March 29, 2012 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. Link to comment https://forums.phpfreaks.com/topic/259939-remove-non-alphanumeric-characters-except-underscore/ Share on other sites More sharing options...
AyKay47 Posted March 29, 2012 Share Posted March 29, 2012 Use preg_replace, erg_replace is deprecated. \w includes alpha-numerical characters and an underscore. $value = preg_replace('\W*', '', $value); Link to comment https://forums.phpfreaks.com/topic/259939-remove-non-alphanumeric-characters-except-underscore/#findComment-1332297 Share on other sites More sharing options...
Nodral Posted March 29, 2012 Author Share Posted March 29, 2012 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? Link to comment https://forums.phpfreaks.com/topic/259939-remove-non-alphanumeric-characters-except-underscore/#findComment-1332319 Share on other sites More sharing options...
AyKay47 Posted March 29, 2012 Share Posted March 29, 2012 Can you give me an example? Also note that I forgot the delimiters in my above reply. Link to comment https://forums.phpfreaks.com/topic/259939-remove-non-alphanumeric-characters-except-underscore/#findComment-1332320 Share on other sites More sharing options...
Nodral Posted March 29, 2012 Author Share Posted March 29, 2012 Delimiters? Sorry, not really used regex before, finding it's a whole different ball game!!! Link to comment https://forums.phpfreaks.com/topic/259939-remove-non-alphanumeric-characters-except-underscore/#findComment-1332323 Share on other sites More sharing options...
Nodral Posted March 29, 2012 Author Share Posted March 29, 2012 Ok, found what you mean. Now I have $value = preg_replace('/\W*/', '', $value); But I still get ÿþcourse_id How can I strip the foreign characters off? Link to comment https://forums.phpfreaks.com/topic/259939-remove-non-alphanumeric-characters-except-underscore/#findComment-1332326 Share on other sites More sharing options...
AyKay47 Posted March 29, 2012 Share Posted March 29, 2012 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; Link to comment https://forums.phpfreaks.com/topic/259939-remove-non-alphanumeric-characters-except-underscore/#findComment-1332339 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.