pioneerx01 Posted February 18, 2011 Share Posted February 18, 2011 I am trying to filter characters that get submitted into forms and than database. I have been paying with trim function $string = $_POST[name]; $newstring = trim($string,"W"); echo "$newstring"; but it does not seem to do what I really need. If I enter name World I do get "orld" back, but what if I want to filter out W (or w) and L (or l) to get "ord". I am mainly going after removing ' " ; : . > , < - _ ( ) * & ^ % $ # @ ! \ | / ? I know there is a different way to do it, but it has been long tome since I have seen it, and I do not know where. Thanks for your help Link to comment https://forums.phpfreaks.com/topic/228070-how-to-filter-what-charachers-get-submitted/ Share on other sites More sharing options...
Pikachu2000 Posted February 18, 2011 Share Posted February 18, 2011 str_replace Link to comment https://forums.phpfreaks.com/topic/228070-how-to-filter-what-charachers-get-submitted/#findComment-1176077 Share on other sites More sharing options...
EmlynK Posted February 18, 2011 Share Posted February 18, 2011 You could put all the characters into an array: $characters = array("'","\"",";",":",".",">",",","<","-","_","(",")","*","&","^","%","$","#","@","!","\\","|","/","?"); And then use a foreach loop to run through each one, replacing them with "" using str_replace(). Link to comment https://forums.phpfreaks.com/topic/228070-how-to-filter-what-charachers-get-submitted/#findComment-1176201 Share on other sites More sharing options...
Pikachu2000 Posted February 18, 2011 Share Posted February 18, 2011 You could put all the characters into an array: $characters = array("'","\"",";",":",".",">",",","<","-","_","(",")","*","&","^","%","$","#","@","!","\\","|","/","?"); And then use a foreach loop to run through each one, replacing them with "" using str_replace(). No need for a loop. str_replace will accept arrays as arguments. search The value being searched for, otherwise known as the needle. An array may be used to designate multiple needles. replace The replacement value that replaces found search values. An array may be used to designate multiple replacements. Link to comment https://forums.phpfreaks.com/topic/228070-how-to-filter-what-charachers-get-submitted/#findComment-1176233 Share on other sites More sharing options...
EmlynK Posted February 18, 2011 Share Posted February 18, 2011 Ahh, thank you for pointing that out. I hadn't realised that. You learn something new every day. Link to comment https://forums.phpfreaks.com/topic/228070-how-to-filter-what-charachers-get-submitted/#findComment-1176353 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.