SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, can anyone spot any glaring error(s) in the below script - I'm trying to get just the first 'links' printed out from a test source. There are no errors coming up - just a blank page so I don't know what wrong;

    Source page http://www.chillisauce.co.uk/testfrom.htm

    Code being used:

    <?

    ########################
    ## Mandatory Setting ##
    ########################

    $GrabURL = "http://www.chillisauce.co.uk/testfrom.htm"; //- Complete URL of the page your grabbing from!
    $GrabStart = "<!--This is where I want the grab to start -->";
    $GrabEnd = "<!--This could be used as grab end -->";
    $Linkstart = "<p>";
    $Linkend = "<br>";
    $Textstart = "<font color";
    $Textend = "</p>";
    $Numberoflinkstoprint = "3";

    ########################################
    ## Edit str_replace if required below ##
    ########################################
    $file = fopen("$GrabURL", "r");
    $rf = fread($file, 20000);
    $grab = eregi("$GrabStart(.*)$GrabEnd", $rf, $printing);
    // $printing[1] = str_replace("", "", $printing[1]);
    $pieces = explode("$Linkstart", $printing[0]);
    $count=count($pieces);
    for($x=1;$x<=$Numberoflinkstoprint;$x++){
    eregi("$Linkstart(.*)$Linkend",$pieces[$x],$Links );
    eregi("$Textstart(.*)$Textend",$pieces[$x],$Text );
    echo $Links[1];
    //echo$Text [1]; Unmark this line to print text
    }
    fclose($file);
    ####################
    ## End of Script ##
    ####################
    ?>



  2. #2
    SitePoint Member
    Join Date
    Nov 2000
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it looks like some of your variables are in fact being called, but they are in the form of literals! i am new to PHP, so i could be wrong.

    gumstic

  3. #3
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Humm - Well I a complete novice - put this script together as a mixture of several others. Does anyone have a solution if my variables are in the form of literals!

    Simplified answers please as I'm not conversant with tech speak yet!

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I cahnged a few lines in your script around and tried to just comment out the ones that I changed so you could see where I did things differently but here goes. A couple of things that were causing you an issue were that you explode the string on the <p> tag so that takes the <p> tag out of the elements of the newly formed array there fore you can't eregi everything from <p> to <br> same for the $startLink var it is "<font color" this will mean that it will take everything after the = sign which would be rather ugly in html. So what I did is take everything up to the <br> in the first ereg then everything after the <br> tag in the second check it oput below:

    <?

    ########################
    ## Mandatory Setting ##
    ########################

    $GrabURL = "http://localhost/sample.htm"; //- Complete URL of the page your grabbing from!
    $GrabStart = "<!--This is where I want the grab to start -->";
    $GrabEnd = "<!--This could be used as grab end -->";
    $Linkstart = "<p>";
    $Linkend = "<br>";
    $Textstart = "<font color=\"";
    $Textend = "</p>";
    $Numberoflinkstoprint = "3";

    ########################################
    ## Edit str_replace if required below ##
    ########################################
    $file = file($GrabURL);
    //$file = fopen("$GrabURL", "r");
    //$rf = fread($file, 20000);
    $lines = implode("", $file);
    $grab = eregi("$GrabStart(.*)$GrabEnd", $lines, $printing);
    // $printing[1] = str_replace("", "", $printing[1]);
    $pieces = explode($Linkstart, $printing[1]);
    //$pieces = explode($Linkstart, $printing[0]);
    $count=count($pieces);
    for($x=1;$x<=$Numberoflinkstoprint;$x++){
    eregi("(.*)$Linkend",$pieces[$x],$Links );
    eregi("$Linkend(.*)$Textend",$pieces[$x],$Text );
    echo $Links[1]."<br>";
    echo $Text[1]."<br>";
    }
    //fclose($file);
    ####################
    ## End of Script ##
    ####################
    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheer Freddy - I'm sure your cat does PHP!

    I didn't realise the ergi function remove those tags! I'm studying it hard to see what you did.

    Couple of quick questions.

    Why did you change the file open and file read to the implode function - is it better in anyway? Is there a way to limit the amount of data that is read in this method to avoid downloading unnecessary data?

    Presumably I can still use this line in the script:
    // $printing[1] = str_replace("", "", $printing[1]);
    if required to remove stuff?

    I would also like to make this line work with an array which I could enter all the stuff I didn't want. eg

    $Removeunwanted = array("<table>", "<td>", "<tr>", "</table>", "</td>", "</tr>",)

    Then use
    $printing[1] = str_replace("$Removeunwanted", "", $printing[1]);

    When I try this it does not work however - I'm very new to Programming and am therefore likely to be missing something very simple.

    How can I get this to work - do I need to do a for loop to remove each element? or am I missing something dumb?

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes you can use the fopen() and fread() functions, I just prefer no to. It isn't the ereg that takes the <p> out it is the exlode("<p>", $string); that does. With your unwanted tags array you would need a loop like:

    $Removeunwanted = array("<table>", "<td>", "<tr>", "</table>", "</td>", "</tr>",)

    for($i=0;$i<count($Removeunwanted);$i++) {
    $printing[1] = str_replace($Removeunwanted[$i], "", $printing[1]);
    }

    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Brilliant

    Thanks Loads!!!!!!!!!!!

  8. #8
    SitePoint Member
    Join Date
    Nov 2000
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    may compliments go to freddydoephp. i have seen his PHP-related posts in other forums and he knows what the heck he is talking about. we are very lucky to have him on this forum.

    gumstic


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •