New: how to search a variable for strings in array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
marcelcolt
Forum Newbie
Posts: 22
Joined: Tue Mar 02, 2010 5:56 am

New: how to search a variable for strings in array

Post by marcelcolt »

First of all: Hi everybody :D

Now to the point:
This is probably a real rookie-question, i know.
I have a piece of PHP-code (mail-form) and i want to exclude some characters from being entered (aka "header-injection").
Now i found all characters not-available for mail-addresses and i put those in an array (For example [, ] and \).
I know how to search a variable for a certain string (strstr or stristr).
But is it possible to search for all entered string in an array?

Something like: stristr_fromArray ($var, $array){ Do something }

I hope you can help!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: New: how to search a variable for strings in array

Post by AbraCadaver »

Not exactly what you asked for, but have you looked at filter_var()?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
marcelcolt
Forum Newbie
Posts: 22
Joined: Tue Mar 02, 2010 5:56 am

Re: New: how to search a variable for strings in array

Post by marcelcolt »

Hi, sorry for the late reply, i've been busy.

I've looked into it and found that preg_match might just do the trick.
Only problem is that i am packing the string to use with special characters like \, [, ], ( and ).
How can i input them in a string?

This is what i got sofar (and sofar still working):

Code: Select all

$checkvalidmail = "/[.;:<>]/i";
Hope you can help.
fireproofsocks
Forum Newbie
Posts: 4
Joined: Thu Mar 04, 2010 3:04 am

Re: New: how to search a variable for strings in array

Post by fireproofsocks »

Look up regular expressions for valid emails... there are a lot out there. The idea is basically what you're after, except it's the standardized framework/functions to support it. Instead of an array, you would define a class of disallowed characters for preg_match() for example.

The Perl regular expressions are what I'd recommend learning... Perl practically wrote the book on regex's and the format translates to a lot of other languages, e.g.

Code: Select all

if ( preg_match('/[^a-z0-9_\_\@\.]/',$email_string) ) { // fail }
filter_var are interesting... but a bit wonky to use in my opinion.
marcelcolt
Forum Newbie
Posts: 22
Joined: Tue Mar 02, 2010 5:56 am

Re: New: how to search a variable for strings in array

Post by marcelcolt »

But no way to use "()[]\:;," in a string?

:;, are no problem, but the second i use ()[] or \ all fails.
No way to escape them (in one string)?

Untill now i use a stristr per char.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: New: how to search a variable for strings in array

Post by AbraCadaver »

As I stated before, rather than reinvent the wheel, use something built-in:

Code: Select all

filter_var($yourvar, FILTER_VALIDATE_EMAIL);
But to answer your question --- to include special characters in the pattern you need to escape them "/[^\(\)\[\]\\]/". Anything that has special meaning in a regex needs to be escaped with a \. It is probably easier and maybe better to list what IS allowed, not what is NOT allowed "/[a-zA-Z0-9@\._-]/" or whatever.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
marcelcolt
Forum Newbie
Posts: 22
Joined: Tue Mar 02, 2010 5:56 am

Re: New: how to search a variable for strings in array

Post by marcelcolt »

Yeah well...i kinda figured it out (my own way, sorry!).

About reinventing the wheel...well i'm a royal pain in the buttocks and a smart-ass so i wanted to try it my own way.
And so i did.

And this is it (sorry MJ):

Code: Select all

$checkvalidmail = "/[,;:`\"<>()\[\]\\\]/i";
    
    
    if (preg_match ($checkvalidmail, $klant_mail)) {
            echo '<p>Sorry, er is een probleem met uw email-adres.</p>';
                                      //meaning that i found the input 'evil' (Muhahaha :twisted: )
    }
Now i excluded all non-allowed chars in an emailaddress.

Anyone want to try (for me just to make sure):
http://www.serelsnauw.nl/users/marcel/t ... index.html
Post Reply