dsaba Posted June 6, 2007 Share Posted June 6, 2007 I know how to use regex with preg_match() to identify strings that are not alphanumeric but I need to somehow use regex to actually filter strings and make them only alphanumeric this means finding all non alphanumeric characters and replacing them with "" or nothing any idea of how this can be done with regex? -thanks Quote Link to comment https://forums.phpfreaks.com/topic/54498-help-with-simple-filter-regex/ Share on other sites More sharing options...
per1os Posted June 6, 2007 Share Posted June 6, 2007 http://us2.php.net/preg_replace Using preg_match's sister preg_replace. Quote Link to comment https://forums.phpfreaks.com/topic/54498-help-with-simple-filter-regex/#findComment-269557 Share on other sites More sharing options...
dsaba Posted June 6, 2007 Author Share Posted June 6, 2007 I talked with preg_match's sister and we exchanged digits, but I'm not quite sure what to say to her when I call her up? Is this good? preg_replace ( mixed $pattern, mixed $replacement, mixed $subject [, int $limit [, int &$count]] ) $string = 'hello hotty! do you want to*&)*(^&$%)*(&^&%^&$%?'; $string = preg_replace('/[^a-z0-9]i/', '', $string); Quote Link to comment https://forums.phpfreaks.com/topic/54498-help-with-simple-filter-regex/#findComment-269559 Share on other sites More sharing options...
per1os Posted June 6, 2007 Share Posted June 6, 2007 Did you try it and see if it was good? Quote Link to comment https://forums.phpfreaks.com/topic/54498-help-with-simple-filter-regex/#findComment-269560 Share on other sites More sharing options...
dsaba Posted June 6, 2007 Author Share Posted June 6, 2007 yeah and it returned my original string just as it was, with the symbols still there (in other words it did not produce the desired result, I sense an error in my regex, but where..?) Quote Link to comment https://forums.phpfreaks.com/topic/54498-help-with-simple-filter-regex/#findComment-269562 Share on other sites More sharing options...
Caesar Posted June 6, 2007 Share Posted June 6, 2007 <?php $str = 'hello hotty! do you want to*&)*(^&$%)*(&^&%^&$%?'; $str = preg_replace('/[\&\*\_\@\^\%\$\(\)]/',"",$str); echo $str; ?> Etc.... Quote Link to comment https://forums.phpfreaks.com/topic/54498-help-with-simple-filter-regex/#findComment-269572 Share on other sites More sharing options...
dsaba Posted June 6, 2007 Author Share Posted June 6, 2007 thanks for the reply, but this kind of regex i was already aware of let me be a little more clear on what I was trying to do: 1. there are tons of symbols over 50 i want to remove rather than listing all of them like the regex you posted, i simply want to say anything that is not letters and not numbers how can this be done? this is what I tried with no success: $string = 'hello hotty! do you want to*&)*(^&$%)*(&^&%^&$%?'; echo preg_replace('/^a-z0-9/i', '', $string); how do I fix it? Quote Link to comment https://forums.phpfreaks.com/topic/54498-help-with-simple-filter-regex/#findComment-269575 Share on other sites More sharing options...
Caesar Posted June 6, 2007 Share Posted June 6, 2007 Try... <?php $str = 'hello hotty! do you want to*&)*(^&$%)*(&^&%^&$%?'; preg_match_all('/[\w]+/', $str, $filtered_string); print_r($filtered_string); //Should throw all alphanumeric words into an array ?> Quote Link to comment https://forums.phpfreaks.com/topic/54498-help-with-simple-filter-regex/#findComment-269584 Share on other sites More sharing options...
dsaba Posted June 6, 2007 Author Share Posted June 6, 2007 ok i realized that I needed to solely filter out the symbols and not just anything thats not alphanumeric, since a lot of my strings will be in other languages (which is not a-z) and I don't want to filter out that text in other alphabets... so I am trying this: (yes I know regex can do it in less lines, but hell I do what I can and what I know..) $badsymbolArray[] = "!"; $badsymbolArray[] = "@"; $badsymbolArray[] = "#"; $badsymbolArray[] = "$"; $badsymbolArray[] = "%"; $badsymbolArray[] = "^"; $badsymbolArray[] = "&"; $badsymbolArray[] = "*"; $badsymbolArray[] = "("; $badsymbolArray[] = ")"; $badsymbolArray[] = "-"; $badsymbolArray[] = "_"; $badsymbolArray[] = "="; $badsymbolArray[] = "+"; $badsymbolArray[] = "~"; $badsymbolArray[] = "`"; $badsymbolArray[] = ":"; $badsymbolArray[] = ";"; $badsymbolArray[] = "'"; $badsymbolArray[] = "\""; $badsymbolArray[] = "["; $badsymbolArray[] = "]"; $badsymbolArray[] = "{"; $badsymbolArray[] = "}"; $badsymbolArray[] = "|"; $badsymbolArray[] = "\\"; $badsymbolArray[] = "/"; $badsymbolArray[] = "?"; $badsymbolArray[] = ","; $badsymbolArray[] = "<"; $badsymbolArray[] = ">"; $badsymbolArray[] = "."; $string = "he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want to*&)*(^&764564$%)*(&^&94534%^&$%?"; foreach ($badsymbolArray as $key => $value) { $newString = str_replace($value, "REPLACED", $string); echo $newString.'<br>'; } $stringArray = explode(' ', $newString); print_r($stringArray); this is the output: he789CAPITALllo hottyREPLACED CAPTIALNESSREPLACED do y7895ou want to*&)*(^&764564$%)*(&^&94534%^&$%? he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want to*&)*(^&764564$%)*(&^&94534%^&$%? he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want to*&)*(^&764564$%)*(&^&94534%^&$%? he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want to*&)*(^&764564REPLACED%)*(&^&94534%^&REPLACED%? he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want to*&)*(^&764564$REPLACED)*(&^&94534REPLACED^&$REPLACED? he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want to*&)*(REPLACED&764564$%)*(&REPLACED&94534%REPLACED&$%? he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want to*REPLACED)*(^REPLACED764564$%)*(REPLACED^REPLACED94534%^REPLACED$%? he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want toREPLACED&)REPLACED(^&764564$%)REPLACED(&^&94534%^&$%? he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want to*&)*REPLACED^&764564$%)*REPLACED&^&94534%^&$%? he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want to*&REPLACED*(^&764564$%REPLACED*(&^&94534%^&$%? he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want to*&)*(^&764564$%)*(&^&94534%^&$%? he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want to*&)*(^&764564$%)*(&^&94534%^&$%? he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want to*&)*(^&764564$%)*(&^&94534%^&$%? he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want to*&)*(^&764564$%)*(&^&94534%^&$%? he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want to*&)*(^&764564$%)*(&^&94534%^&$%? he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want to*&)*(^&764564$%)*(&^&94534%^&$%? he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want to*&)*(^&764564$%)*(&^&94534%^&$%? he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want to*&)*(^&764564$%)*(&^&94534%^&$%? he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want to*&)*(^&764564$%)*(&^&94534%^&$%? he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want to*&)*(^&764564$%)*(&^&94534%^&$%? he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want to*&)*(^&764564$%)*(&^&94534%^&$%? he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want to*&)*(^&764564$%)*(&^&94534%^&$%? he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want to*&)*(^&764564$%)*(&^&94534%^&$%? he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want to*&)*(^&764564$%)*(&^&94534%^&$%? he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want to*&)*(^&764564$%)*(&^&94534%^&$%? he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want to*&)*(^&764564$%)*(&^&94534%^&$%? he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want to*&)*(^&764564$%)*(&^&94534%^&$%? he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want to*&)*(^&764564$%)*(&^&94534%^&$%REPLACED he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want to*&)*(^&764564$%)*(&^&94534%^&$%? he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want to*&)*(^&764564$%)*(&^&94534%^&$%? he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want to*&)*(^&764564$%)*(&^&94534%^&$%? he789CAPITALllo hotty! CAPTIALNESS! do y7895ou want to*&)*(^&764564$%)*(&^&94534%^&$%? Array ( [0] => he789CAPITALllo [1] => hotty! [2] => CAPTIALNESS! [3] => do [4] => y7895ou [5] => want [6] => to*&)*(^&764564$%)*(&^&94534%^&$%? ) so to keep it simple, the str_replace() is not removing the symbols, why? and how can I fix it? at this point I don't care if a solution is in regex with preg_replace or with str_replace, I just want to get it done... (there are 31 "bad" symbols I want to remove) -thanks Quote Link to comment https://forums.phpfreaks.com/topic/54498-help-with-simple-filter-regex/#findComment-269592 Share on other sites More sharing options...
fert Posted June 6, 2007 Share Posted June 6, 2007 try foreach ($badsymbolArray as $value) { $newString = str_replace($value, "REPLACED", $string); echo $newString.'<br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/54498-help-with-simple-filter-regex/#findComment-269603 Share on other sites More sharing options...
per1os Posted June 7, 2007 Share Posted June 7, 2007 Something like this might help you out: $convUrl = ereg_replace("'|\.|\?|!|,|\"|&|:|-|\[|\]|\(|\)|\+|=|~|\||\*|\^|%|\$|@|#|<|>|`|;|_|\{|\}", "", $convUrl); I use it to strip out bad lines to make subjects seo friendly. Quote Link to comment https://forums.phpfreaks.com/topic/54498-help-with-simple-filter-regex/#findComment-269727 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.