finding and subtracting a string from within a large string in php


plainly n simply:

I want to use a php script to issue a wget command in linux to download a page into a string in php.
From this string, I wish to single out a line containing the string "Start time:</td><td>22-Nov-04 20:35:00".

From this string, I wish to find 1) the date and 2) the time that this countdown started.


thanks very much

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

First of all, you don't need to use wget to download the html of a webpage. you can do it with php only. My example doesn't work with url's that contain query strings. However if you run into any trouble tell me because you can overcome it with curl.

Supposing that after that string there is a "</td>" (could be anything, just replace $end_str with it):

function extract_url($url)
   $filename = $url;
   $handle = fopen ($filename, "r");
   $content = fread ($handle, filesize ($filename));
   fclose ($handle);
   return $content;
$str = extract_url("");
$start_str = "Start time:</td><td>";
$end_str = "</td>";
$begin = str_pos($str, $start_str);
$end = str_pos($str,$end_str, $begin);

$final = substr($str, $begin+strlen($start_str), $end);
$arr = explode(" ",$final);

$date = $arr[0];
$time = $arr[1];

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
If the string stays constant(meaning length size)

you could just use the substr command to get what you want

$mystr = "Start time:</td><td>22-Nov-04 20:35:00";

$date = substr("$mystr", 20, 9);
$time = substr("$mystr", 30, 8);
If you wanted to use wget for a particular reason (ie. command-line arguments) you can use PHP's popen function.

For example:

     $f = popen('wget','r');

The resource returned by popen works just like the one returned by fopen.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.