Jump to content

How to filter what charachers get submitted.


pioneerx01

Recommended Posts

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

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().

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.