-1

I want to find the index of the pattern from the string and find all Unicode characters like "\u2026","\u2021","\u2011" and so on.

Below is the code snippet I am currently using. But it is resulting in else situation.

if(preg_match('/[\\^]u[0-9]{4}/gi',$data['title'],$matches,PREG_OFFSET_CAPTURE)){
     print_r($matches);
}
else{
     echo "Not Found";
}

Thanks.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563

1 Answers1

1

There are several issues with your code.

  1. If you're using single quotes for the pattern and want to match a literal backslash, you need to use at least \\\ or even \\\\ to produce an escaped backslash \\. Just echo your pattern if unsure.

  2. Instead of using the global flag g which is not available in PHP use preg_match_all. If it matches, it returns the number of matches. You can check match condition by preg_match_all(...) > 0

  3. Unsure about ^ in [\\^]. if you don't need it, drop it. Further [0-9] can be reduced to \d. Also I would add a word boundary \b after \d{4} if something like \u12345 should not be matched.

See this PHP demo at tio.run

$pattern = '/\\\u\d{4}\b/i';
# echo $pattern;

if(preg_match_all($pattern, $data['title'], $matches, PREG_OFFSET_CAPTURE) > 0){
  print_r($matches[0]);
} else{
  echo "Not Found";
}
bobble bubble
  • 16,888
  • 3
  • 27
  • 46