[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2004-11-29
Medium Priority
Last Modified: 2008-01-09

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

Question by:daryn

Accepted Solution

b_loco earned 400 total points
ID: 12700473
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("http://www.example.com/test.htm");
$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];

Assisted Solution

jmsloan earned 400 total points
ID: 12717525
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);

Assisted Solution

cmaron earned 200 total points
ID: 12803753
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 http://www.website.com/file.html','r');

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


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month19 days, 17 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question