gfhgf
gfhgf

Reputation: 11

Find word which contains a specified group of letters REGEXP

I want to find all the words that contain a specified group of letters, for example if I want to search the with the group of letter ph int the text phone find phill phdas I want the REGEXP to return me phill phone phdas I dont want to do it in another way than REGEXP. (PHP)

Upvotes: 0

Views: 62

Answers (3)

Salim Ibrohimi
Salim Ibrohimi

Reputation: 1391

Try this:

<?php
  $subject = "your subject";
  $pattern = '//\w*ph\w*//';
  preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
  print_r($matches);
?>

Upvotes: 0

anubhava
anubhava

Reputation: 785196

You can use this regex:

/\w*ph\w*/

That will match 0 or more word characters, followed by your search term ph, followed by 0 or more word characters.

RegEx Demo

PHP Code:

$kw = 'ph';
preg_match_all('/\w*' . $kw . '\w*/', $str, $matches);

Upvotes: 1

Chandan Suri
Chandan Suri

Reputation: 598

You can use the Pregmatch function of php and as it's first argument give the regex as /\wph\w/. Give stars after the w as well , that is for any character present..

Upvotes: 0

Related Questions