1

trying to get Textrange (n words before and after) a search string (myself)

$text = 'Me, my dog and “myself“ are going on a vacation. Irene and myself are broke. Myself is here :P John and myself!';

 preg_match_all("/(?:[^ ]+ ){0,2}(?:[“'"(‘. ])myself(?:[“'")‘. ])(?: [^ ]+){0,2}/", $text, $matches);   

this gives me matches :

• dog and “myself“ are going

• myself

But it should be:

• dog and “myself“ are going

• Irene and myself are broke

• John and myself!

Please help me find all matches as text range 2 words before and 2 words after. no matter if there is a special char or whitespace before or after search string (myself) or 'myself' or “myself“ ...

thanks.Sepp

6
  • you want to get two word before "myself" and two word after "myself" ? Commented Apr 4, 2019 at 10:51
  • yes. no matter if there is a special char or whitespace before or after. Commented Apr 4, 2019 at 10:53
  • Does it have to be regex? Commented Apr 4, 2019 at 10:54
  • yes it has to be regex Commented Apr 4, 2019 at 10:54
  • Any kind of regex or only preg_match? Commented Apr 4, 2019 at 10:57

1 Answer 1

1

The problem arises due to the fact that both [“'"(‘. ] and [“'")‘. ] are obligatory and require one char to be there before and after myself. Then, there must also be another space before and after myself required by (?:[^ ]+ ){0,2} and (?: [^ ]+){0,2}.

You may use

'/(?:\S+\s+){0,2}(?:[“'"(‘.])?myself(?:[“'")‘.]?)(?:\s+\S+){0,2}/u'

Or allow any punctuation around myself with \p{P}:

'/(?:\S+\s+){0,2}\p{P}?myself\p{P}?(?:\s+\S+){0,2}/u'

See the regex demo

Note that (?:[“'"(‘.])? and (?:[“'")‘.]?) (or \p{P}?) are all optional, the ? quantifier after them makes the regex engine match only 1 or 0 occurrences of these patterns. So, if it is there or not, the match occurs.

PHP demo:

$text = 'Me, my dog and “myself“ are going on a vacation. Irene and myself are broke. Myself is here :P John and myself!';
if (preg_match_all('/(?:\S+\s+){0,2}\p{P}?myself\p{P}?(?:\s+\S+){0,2}/u', $text, $result)) {
    print_r($result[0]);
}

Output:

Array
(
    [0] => dog and “myself“ are going
    [1] => Irene and myself are broke.
    [2] => John and myself!
)
Sign up to request clarification or add additional context in comments.

1 Comment

as far as i see thats working as expected. thanks a lot! sepp.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.