php - Only Alphanumeric and one special charcter regex in preg_match -


$str = 'test-test-test-test\\50-test-37447';      if(preg_match('/[a-za-z0-9-]/',$str,$matches)){          echo "<pre>true";print_r($matches);echo "</pre>";      }else{          echo "<pre>false";print_r($matches);echo "</pre>";      } 

it return true? wrong?

it returns true because regex calls 1 character set (so t start first match).

if want entire string made of characters, use:

^[a-za-z0-9-]*$ 

the ^ , $ markers indicate start , end of string respectively, , * means 0 or more of preceding "object". hence means entire string must made of 0 or more of character set you've specified.

this causes \ character fail match in test string.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -