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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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.