rewrite links to use reffere script

Discussion in 'PHP' started by azarat, Jul 15, 2010.

  1. #1
    Hi,

    What I want to do is to take a link like this http://test.com and make it into http.//www.mydomain.com/redirect.php?site=test.com

    I got my data in a string (they need to be submitted to a database). So I've tried using str_replace like this:

    $content2 = str_replace("http://", "http.//www.mydomain.com/redirect.php?mid={$msg_id}&site=", $content);
    PHP:
    however what I get from this is a link to
    http://localhost:8888/%22http://www.mydomain.com/redirect.php?site=test.com\%22
    Code (markup):
    Which I really dont get (note I'm currently testing on my localhost..)

    Someone can help me get this done correctly?

    Thanks a lot
    Jacob / AzaraT
     
    azarat, Jul 15, 2010 IP
  2. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    <?php
        $url = 'http://test.com';
        
        preg_match('|^(?:http://)?(www\.)?([^/]+)|i', $url, $matches);
        $newURL = 'http.//www.mydomain.com/redirect.php?site=' . $matches[2];
        
        echo $newURL;
    ?>
    
    PHP:
     
    Deacalion, Jul 15, 2010 IP
  3. azarat

    azarat Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks,

    however it doesn't work. What I got now is this.

    $content = '<a href="http://test.com">test</a> <p> Other html code here blablabla </p>';
    preg_match('|^(?:http://)?(www\.)?([^/]+)|i', $content, $matches);
    $newURL = 'http//www.mydomain.com/redirect.php?site=' . $matches[2];
    echo $newURL;
    PHP:
    and it returns:
    http//www.mydomain.com/redirect.php?site=
    Code (markup):
    What it needed to return was:
    <a href="http//www.mydomain.com/redirect.php?site=test.com">test</a> <p> Other html code here blablabla </p>
    HTML:
     
    azarat, Jul 15, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    <?php
    function callback($match) {
    return 'http://www.mydomain.com/redirect.php?site='.parse_url($match[1], PHP_URL_HOST);
    }
    
    $content = '<a href="http://test.com">test</a> <p> Other html code here blablabla </p>';
    
    //this is where the changes take effect...
    $content = preg_replace_callback('~(http://[^ "]+)~i', 'callback', $content);
    
    echo $content;
    ?>
    PHP:
     
    Last edited: Jul 15, 2010
    danx10, Jul 15, 2010 IP
  5. azarat

    azarat Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks a lot, that worked :)
     
    azarat, Jul 15, 2010 IP
  6. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Ahh... didn't realise it would be embedded in HTML. :)
     
    Deacalion, Jul 15, 2010 IP
  7. azarat

    azarat Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Might not have been specific enough. Thanks for your help anyway :D
     
    azarat, Jul 15, 2010 IP
  8. azarat

    azarat Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hi again :)

    As said this worked, however there is one small issue. If I have this <a href="http://test.com">http://test.com</a>

    It also rewrites the text that is displayed, I only want it to change the href atrribute, and not other things. Eg if the content is:

    $content = '<a href="http://test.com">http://test.com</a> <p> Other html code here blablabla </p>';
    PHP:
    it should return:

    <a href="http//www.mydomain.com/redirect.php?site=test.com">http://test.com</a> <p> Other html code here blablabla </p>
    HTML:
    Someone can help me with this? I really don't understand this :s eg all the special charectors in the preg_replace_callback :)

    Thanks :)
     
    azarat, Aug 12, 2010 IP
  9. Mythotical

    Mythotical Well-Known Member

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    111
    #9
    The way I would handle this is assign the clicked linked a variable such as below:
    
    <a href="http//www.mydomain.com/redirect.php?site=test.com" name="test">http://test.com</a>
    
    PHP:
    Then in this code:
    
    $test = $_GET['name'];
    function callback($match) {
    return 'http://www.mydomain.com/redirect.php?site='.parse_url($match[1], PHP_URL_HOST);
    }
    
    $content = '<a href="http://test.com">test</a> <p> Other html code here blablabla </p>';
    
    //this is where the changes take effect...
    $content = preg_replace_callback('~(http://[^ "]+)~i', 'callback', $content);
    
    echo $content;
    ?>
    
    PHP:
    Notice I added at the top to call the variable since it can still be passed as data.

    This is untested as I haven't worked with passing data from a href to a referrer file.
     
    Mythotical, Aug 12, 2010 IP
  10. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #10
    change it like
    
    $content = preg_replace_callback('~href="(http://[^ "]+)~i', 'callback', $content);
    
    PHP:
     
    gapz101, Aug 12, 2010 IP
  11. azarat

    azarat Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    @gapz101
    That worked. I have already tired that but for some reason I used " instead of ' in the callback function and that seems to have bugged it or something. Weird :)

    But thanks :D
     
    azarat, Aug 13, 2010 IP