grab succeeding string

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Say I have a chunk of text and I know that somewhere in that chunk ot
text is the phrase 'Next Week's Winning Number will be:', is there a
way I can automatically grab the string immediately following?

Any help greatly appreciated.

Re: grab succeeding string

strawberry wrote:
Quoted text here. Click to load it

If you know the stringlength of the following string:

$occurance = strpos( $chunk, 'Next Week's Winning Number will be:');
$occurance += strlen('Next Week's Winning Number will be:');
$string = trim(substr($occurance, $lenght_of_searched_string));

If the lenght is undefined, use slower regular expression (allthough added
bonus is we can filter out whitespace-characters):

preg_match("/(?<=Next Week's Winning Number will be:)\s*([^\s]+?)\b/si",
$chunk, $match);

Rik Wasmus

Re: grab succeeding string

On Wed, 10 May 2006 16:43:37 -0700, strawberry wrote:

Quoted text here. Click to load it

Does the following snippet answer your question?

$buff = "Next Week's Lotto: 123456";
if (preg_match("/Next Week's Lotto:\s*(\d+)/", $buff, $hit)) {
    echo "Next week lottery:", $hit[1], "\n";
} else {
    echo "You suck!\n";

Of course, I make no claims that the next week's lottery numbers will be
"123456". No amount of regex massage will help you with that.


Re: grab succeeding string

Thanks Rik and Mladen. That's a great help.

This time next week, I think I'm going to be very, very lucky ;-)

Re: grab succeeding string

Hello again,

For various reasons, tiny little errors in the scripts provided
prohibited them from working (at least on my system). However, they got
me pointed in the right direction and, coupled with a useful
introduction to Regular Expressions at,
I was able to put together the following. I don't know if it's perfect
- but it works for my purposes so I thought I'd share it with everyone:

$watchword = 'Numbers:&nbsp;';

if (preg_match("/(?<=$watchword).*?\s/s",$pagedata, $match)){
echo $string;


Site Timeline