Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/54498-help-with-simple-filter-regex/
Share on other sites

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);

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?

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

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.