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 = "jacket";
preg_match($q,$str,$matches);

print $matches[1];

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

Answers


You can use preg_match as:

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

See it


Need Your Help

iOS Post Photo to Instagram without opening Instagram App

iphone ios share instagram

Is there a way to post a photo from my app to Instagram without opening the Instragram app?

How do I use yum to download software only from Centos 5.4 and not the latest

linux centos rhel

Requirement is to download software from Centos 5.4. When I do yum install , I get the latest version and not the one available for Centos 5.4.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.