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

Posted on 2004-11-29
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
    LVL 2

    Accepted Solution

    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];
    LVL 3

    Assisted Solution

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

    Assisted Solution

    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.


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    The viewer will learn how to count occurrences of each item in an array.
    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.

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now