Get word from string - PHP
I am trying to extract a word that matches a specific pattern from various strings.
The strings vary in length and content.
I want to extract any word that begins with jac from the following strings and populate an array with the full words:
- I bought a jacket yesterday.
- Jack is going home.
- I want to go to Jacksonville.
The resulting array should be [jacket,Jack,Jacksonville]
I have been trying to use preg_match() but for some reason it won't work. Any suggestions???
$q = "jac"; $str = "jacket"; preg_match($q,$str,$matches); print $matches;
This returns null :S. I dunno what the problem is.
You can use preg_match as:
preg_match("/\b(jac.+?)\b/i", $string, $matches);