-2

My problem is similar to this question:

How to find sequenced pattern of characters in a string with PHP?

But I need to extend the regex in order to take under consideration strings that begin with _ (underscore).

So how is this regex '/(\S{2,}?)\1+/' extended in order to match my need?

EX

The string 'parent_parent_parent.parent' should return [_parent] => 2

Community
  • 1
  • 1

2 Answers2

1

If your need is "find all repeating substrings that start with an underscore", just add the underscore to the regular expression: /(_[\S]{2,}?)\1+/

GertG
  • 869
  • 1
  • 8
  • 21
0

Try this

There is a function in PHP called substr_count that did the same trick.

Similar function: mb_substr_count.

$str = "parent_parent_parent.parent";

echo substr_count($str, "_parent"); // 2
Murad Hasan
  • 9,233
  • 2
  • 16
  • 39