To get a case-insensitive word from a regular expression

Suppose I have a line like

$res = "there are many restaurants in the city. Restaurants like xyz,abc. one restaurant like.....";

In the above example we can find restaurant in 3 places. I need count to be 3.

$pattern = '/Restaurant/';
preg_match($pattern, substr($res,10), $matches, PREG_OFFSET_CAPTURE);
print_r($matches);

Another problem. This is due to the above issue. that is, I have text, for example Food & Drinks. I need to combine this word with foodor drinksor seafood... etc., Someone can help me in getting this. Thank you in advance.

+4
source share
4 answers

You can use regex as follows:

$pattern = '/restaurants?/i';

There are two changes to the original regular expression:

  • i - .
  • s? . s . s.

, , , , , :

+2

i . ? s , .

preg_match(), , preg_match_all()

$pattern = '/restaurants?/i';
preg_match_all($pattern, substr($res,10), $matches, PREG_OFFSET_CAPTURE);
print_r($matches[0]);

. working demo

+2

regex guide - .

|in regex means or or ?means 0 or 1 of the previous char or group, so the following template should work for your specification:

$pattern = '/[Rr]estaurants?/
+1
source
  As a solution to your problem please try executing following code snippet

  $url = "http://www.examplesite.com/";
  $curl = new Curl();
  $res = $curl->get($url);
  $pattern = '/Restaurant(s)*/i';
  preg_match($pattern, substr($res,10), $matches, PREG_OFFSET_CAPTURE);
  print_r($matches);
-2
source

All Articles