RegExp: 50 words before and 50 words after a match

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
visionmaster
Forum Contributor
Posts: 139
Joined: Wed Jul 14, 2004 4:06 am

RegExp: 50 words before and 50 words after a match

Post by visionmaster »

Hello,

I'm looking for a pattern which finds a word in a text and returns the word and 50 words before and 50 words after the found word.

Finding 50 characters before and after is no problem:
"|(.{50})".$strWord."(.{50})|is"


Thanks!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Not tested...

Code: Select all

I removed this regexp cos it was useless ;-)
Add as many punctuation marks into the square brackets as you may find stuck to the end of a word...

EDIT: This is better

Code: Select all

'/(ї\S]{50}\s'.$word.'\sї\S]{50})/i'
It'll find anything that isn't whitespace up until a space or tab or newline etc 50 times before the word and 50 times after (case insensitive)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

d11's requires a minimum of 101 "words" in the string. If anything, should make it flexible in where the word can be found.. so switch to {0,50}
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Good point ;-)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

If i'm not mistaken:
"\<" matches the null string at the start of a word.
"\>" matches the null string at the end of the word.

thus leads to something like:

#(\<.*?\>){0, 50}$word(\<.*?\>){0, 50}#
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

\b is also a word-break trigger. Not sure if that'd work though tim. Can always test it! :)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

still drunk from party last night.... forget what i said :)
Post Reply