How do I use preg_match to return an array of all substrings that match (:)?
For example if I have a string that is:
My name is (:name), my dog's name is (:dogname)
I want to use preg_match to return
array("name", "dogname");
I tried using this expression…
preg_match("/\(:(?P<var>\w+)\)/", $string, $temp);
But it only returns the first match.
Can anyone help me?
First off, you want
preg_match_all(find all matches), notpreg_match(check if there are any matches at all).And for the actual regex, the best technique is to search for
(:and then search for any character except for)