#1
  1. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,141
    Rep Power
    120

    How To Alter All HREF In A Page With A Regex?


    I posted my question in the wrong forum earlier; so now I am posting it here. My question is; how do you alter all the href in a document with a regex? I am really bad a regex; so you will have to forgive me for my example.

    This was the original regex I tried to write; but it did not accomplish what I wanted to do either.

    PHP Code:
    <?php

    $page 
    $_GET["page"];

    echo 
    preg_replace("/[http]/i""http://my-subdomain.mydomain.com/my_proxy.php?page=http", require_once($page));

    ?>
    Here is an example that another person on the PHP forum gave me:

    PHP Code:
    <?php

    $page 
    $_GET["page"];

    if (
    $page != "") {
    echo 
    preg_replace("#http[^\s'\"]+#ie""'http://my-subdomain.mydomain.com/my_proxy.php?page=' . urlencode('\\0')", require_once($page)); 
    }

    ?>
    But that did not work either; it did not add my url to all the existing urls in the page. I altered it slightly to include the require_once().


    Can anyone tell me how I would add my url to all the href in the page?

    Example

    Original URL: http://www.someonesdomain.com/page1.php
    My Altered URL: http://my-subdomain.mydomain.com/my_proxy.php?page=http://www.someonesdomain.com/page1.php

    I want to make all the href in my page look like the "My Altered URL" example above.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2008
    Posts
    97
    Rep Power
    18
    If you use snoopy it will do all the work for you and it has been tested and tried....

    require_once("/includes/snoopy.class.php");
    $snoopy = new Snoopy();
    $url = 'http://php.net/';
    // Get the contents
    $snoopy->timed_out = 10;
    $snoopy->fetchlinks($url);
    print $snoopy->results;

    I have not used it but there are options to automatically replace the domains of the links with your own. It can do proxies with usernames and passwords, https, referer, user-agents and more

    cheers,
    Brent
  4. #3
  5. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,141
    Rep Power
    120
    thanks gbrent; i appreciate the heads up about the "snoopy" class; but i would like to see a real basic working example; for learning purposes.
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,870
    Rep Power
    6351
    I put this in your other thread as well. This works perfectly, but you need to include BOTH lines:
    PHP Code:
    $page file_get_contents($_GET["page"]);  


    echo 
    preg_replace("#http[^\s'\"]+#ie""'http://my-subdomain.mydomain.com/my_proxy.php?page=' . urlencode('\\0')"$page); 
    Also note I used [PHPNET="urlencode"]urlencode[/PHPNET] for the URL so it doesn't break. You'll have to decode it when it gets to my_proxy.php

    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,141
    Rep Power
    120
    Thank You ManiacDan - That Worked!!!

    Now my goal is to try to figure out how I am going to go about making this work for login forms that uses the post method. I am going play around with it a little bit and see what I can come up with.
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,870
    Rep Power
    6351
    You should use Snoopy for making POST requests.

    Or you could just install or copy PHPProxy, it really does work fine.

    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo