开发者

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.

For example:

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开发者_如何学Go = "jacket";
preg_match($q,$str,$matches);

print $matches[1];

This returns null :S. I dunno what the problem is.


You can use preg_match as:

preg_match("/\b(jac.+?)\b/i", $string, $matches);

See it


You've got to read the manual a few hundred times and it will eventually come to you.

Otherwise, what you're trying to capture can be expressed as "look for 'jac' followed by 0 or more letters* and make sure it's not preceded by a letter" which gives you: /(?<!\\w)(jac\\w*)/i

Here's an example with preg_match_all() so that you can capture all the occurences of the pattern, not just the first:

$q = "/(?<!\\w)(jac\\w*)/i";
$str = "I bought a jacket yesterday.
Jack is going home.
I want to go to Jacksonville.";

preg_match_all($q,$str,$matches);

print_r($matches[1]);
  • Note: by "letter" I mean any "word character." Officially, it includes numbers and other "word characters." Depending on the exact circumstances, one may prefer \w (word character) or \b (word boundary.)

You can include extra characters by using a character class. For instance, in order to match any word character as well as single quotes, you can use [\w'] and your regexp becomes:

$q = "/(?<!\\w)(jac[\\w']*)/i";

Alternatively, you can add an optional 's to your existing pattern, so that you capture "jac" followed by any number of word characters optionally followed by "'s"

$q = "/(?<!\\w)(jac\\w*(?:'s)?)/i";

Here, the ?: inside the parentheses means that you don't actually need to capture their content (because they're already inside a pair of parentheses, it's unnecessary), and the ? after the parentheses means that the match is optional.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜