I have a RegExp Tester extension in chrome. Running pattern [-,] against search text
“james,bayo -is a good boy” returns 2 matches
however running a preg_match as below in php returns false
<?php
preg_match("[,-]", "james,bayo -is a good boy");
?>
Expecting it to return 2 as did the regExp tester,
I am sure i am missing something
You need regex delimiters, like so:
This will match one of two characters – The comma, or the dash. Also, in order to retrieve the matches, you need to specify the 3rd parameter to
preg_match(), as that it where your matches will be stored.Note that since you’re using
preg_match(), you’ll only get the first match. So, as is, this will output:If you want all of the matches, you need to use
preg_match_all(), like this:This outputs: