user3829086
user3829086

Reputation: 363

How to return the special character found in string using PHP

I want to get the special chars found in string using PHP code, Here is my trial code:

 $page_num =">256";
 if(preg_match('^[-<>]+$', $page_num))   
 {
        //Here it should returns special chars present in string..
 }

Upvotes: 0

Views: 51

Answers (2)

Niroj Adhikary
Niroj Adhikary

Reputation: 1835

check this

$page_num =">256";
 if(preg_match_all('/\W/', $page_num,$matches))
 {
       print_r($matches);
 }

Upvotes: 2

Niranjan N Raju
Niranjan N Raju

Reputation: 11987

What you have missed is ending delimiter

$page_num =">256";
 if(preg_match('^[-<>]+$^', $page_num))   
                        ^// this one
 {
    //Here it should returns special chars present in string..
 } 

Demo of your code, here

or you can try this,

$string = 'your string here';

if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $string))
{
    echo "yes";
} else {
    echo "no";
}

One more solution is

preg_match('![^a-z0-9]!i', $string);

Upvotes: 1

Related Questions