For example:
$s1 = \"Test Test the rest of string\"
$s2 = \"Test the rest of string\"
I would like to match positively $s1
but
This does not cause Test Testx
to return true.
$string = "Test Test";
preg_match('/^(\w+)\s+\1(\b|$)/', $string);
Not working everywhere, see the comments...
^([^\b]+)\b\1\b
^(\B+)\b\1\b
Gets the first word, and matches if the same word is repeated again after a word boundary.
~^(\w+)\s+\1(?:\W|$)~
~^(\pL+)\s+\1(?:\PL|$)~u // unicode variant
\1
is a back reference to the first capturing group.
if(preg_match('/^(\w+)\s+\1\b/',$input)) {
// $input has same first two words.
}
Explanation:
^ : Start anchor
( : Start of capturing group
\w+ : A word
) : End of capturing group
\s+ : One or more whitespace
\1 : Back reference to the first word
\b : Word boundary