Connecting Tech Pros Worldwide Help | Site Map

grab succeeding string

strawberry
Guest
 
Posts: n/a
#1: May 11 '06
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.

Rik
Guest
 
Posts: n/a
#2: May 11 '06

re: grab succeeding string


strawberry wrote:[color=blue]
> 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.[/color]


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);
$string=$match[1];

Grtz,
--
Rik Wasmus


Mladen Gogala
Guest
 
Posts: n/a
#3: May 11 '06

re: grab succeeding string


On Wed, 10 May 2006 16:43:37 -0700, strawberry wrote:
[color=blue]
> 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.[/color]

Does the following snippet answer your question?

#!/usr/local/bin/php
<?php
$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.

--
http://www.mgogala.com

strawberry
Guest
 
Posts: n/a
#4: May 11 '06

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 ;-)

strawberry
Guest
 
Posts: n/a
#5: May 13 '06

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
http://www.sunflowerroad.com/blog/ti...torial-part-1/,
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)){
$string=$match[0];
echo $string;

Regards

Closed Thread


Similar PHP bytes