473,403 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

grab succeeding string

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.

May 10 '06 #1
4 1235
Rik
strawberry wrote:
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.

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
May 11 '06 #2
On Wed, 10 May 2006 16:43:37 -0700, strawberry wrote:
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.


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

May 11 '06 #3
Thanks Rik and Mladen. That's a great help.

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

May 11 '06 #4
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

May 13 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: gregory_may | last post by:
Is there a way to grab a "Screen Shot" that includes "Tool Tips"? I saw this code someplace, cant remember where. But it doesnt grab "Tool Tips". Is there a better way to do this in .net?...
1
by: gregory_may | last post by:
Is there a way to grab a "Screen Shot" that includes "Tool Tips"? I saw this code someplace, cant remember where. But it doesnt grab "Tool Tips". Is there a better way to do this in .net?...
10
by: gregory_may | last post by:
Is there a way to grab a "Screen Shot" that includes "Tool Tips"? I saw this code someplace, cant remember where. But it doesnt grab "Tool Tips". Is there a better way to do this in .net?...
3
by: Darrel | last post by:
using Request.FilePath will return the entire file path: directory/directory/myfile.aspx Is there a built function in ASP.net to grab just the filename (myfile.aspx) or do I need to write...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.