Jump to content

preg_match or regx + finding "\" foreign chrs in form entrys


etnastyles

Recommended Posts

help please,

 

all this regex and preg_match is very confusing - i have been searching the net for examples but most of the ones i find relate to url searching or the [a-z] classic .

 

 

what im looking for or help with is finding characters that arent meant to be in a form that can be submitted to a mysql db i.e. \, *, +, =, -, !, ? etc.. a backslash \ preg_match would be

gd.

 

could anyone give me a pinter or point me in the correct direction.

 

thks

 

<pre>
<?php
echo preg_match('/[^a-zA-Z0-9]/', 'Invalid string!') ? 'Invalid' : 'Valid' ;
?>
</pre>

matching numbers,characters, digits dont need regex we have ctype data to check this stuff

 

if(ctype_alnum($testcase)){
   echo 'valid';

}else{
   echo 'invalid';
}

php5 can also check email address and URL using predefined functions without using regex

this dosnt check for werid freaky symbols like this |-_-|

 

isnt preg_match therefore better???

 

 

------------------------------code-------------------------------

<?php

$strings = array('1820.20', '10002', 'wsl!12');

foreach ($strings as $testcase) {

    if (ctype_digit($testcase)) {

        echo "The string $testcase consists of all digits.\n";

    } else {

        echo "The string $testcase does not consist of all digits.\n";

    }

}

this code

$strings = array('1820.20', '10002', 'wsl!12','|| ');
foreach ($strings as $testcase) {
    if (ctype_digit($testcase)) {
        echo "true $testcase <br/>";
    } else {
        echo "false $testcase <br/>";
    }
}
?>

gave me this output

false 1820.20

true 10002

false wsl!12

false |-_-|

 

whats wrong with that...

I'm not saying don't use regex but dont over use regex if you only need to check numbers caracters etc..

you need php predefined functions

 

i only use regex when i'm checking for a string on pattern

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.