include

Discussion in 'PHP' started by dean5000v, Jun 9, 2008.

  1. dean5000v

    dean5000v Peon

    Messages:
    201
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #21
    oo yeh sorry thats me playing around with the page trying to get preg_replace to work , so at the moment im tryin to find a link with http://s so matches any link that starts with http://safetynet and then i want to replace it so adds in a sub domain. such as shop. so end product looks like http://shop.safetynet.

    heres a lil script ive tried so far i got the preg match working now im tryin to get the replace bit towork within the statement.

    <?php
    $pattern = '(http://s)';
    $replacement = '(http://shop.s)';
    $file = file_get_contents('http://shop.safetynetdirect.com/cgi-bin/mk_cat.pl?cat=SSHPDG');
    if (preg_match ($pattern, $file, $matches)) {
    echo preg_replace($pattern, $replacement, $file);

    } else {
    echo $file;
    }
    ?>
     
    dean5000v, Jun 9, 2008 IP
  2. lui2603

    lui2603 Peon

    Messages:
    729
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #22
    Try this:

    
    $file = file_get_contents('http://shop.safetynetdirect.com/cgi-bin/mk_cat.pl?cat=SSHPDG');
    preg_replace('/<img src="\/.*?>/si', '<img src="http://originalurl.bla/', $file);
    echo $file;
    
    PHP:
    replace originalurl.bla with the domain hosting the images
     
    lui2603, Jun 9, 2008 IP
  3. dean5000v

    dean5000v Peon

    Messages:
    201
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #23
    all i need to do is add in the subdomain shop onto all of the links to get them to work.

    so http://shop.safetynetdirect.com instead of what it is atm http:/safetynetdirect.com
     
    dean5000v, Jun 9, 2008 IP
  4. dean5000v

    dean5000v Peon

    Messages:
    201
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #24
    ive added this just to match

    <?php
    $pattern = '(http://s)';
    $file = file_get_contents('http://shop.safetynetdirect.com/cgi-bin/mk_cat.pl?cat=SSHPDG');
    if (preg_match_all("(http://s)", $file, $matches)) {
    print_r($matches);
    } else
    {
    echo $file;
    }
    ?>

    but for some reason when i echo the matches value isnt echoing all of the links, when i know there is alot more links on the website then its matching to.
     
    dean5000v, Jun 9, 2008 IP
  5. lui2603

    lui2603 Peon

    Messages:
    729
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #25
    if you do:

    preg_match_all('/<a href="\/.*?>si/',$file,$matches);
    print_r($matches);

    You will see the other links..
    It's because links aren't pointing to http:/s... but /cgi-bin/...
     
    lui2603, Jun 9, 2008 IP
  6. dean5000v

    dean5000v Peon

    Messages:
    201
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #26
    multiple preg_replace in one statement, i got this to work.

    <?php
    $pattern = '(/cgi-bin/)';
    $replacement = '(http://shop.s)';
    $file = file_get_contents('http://shop.safetynetdirect.com/cgi-bin/mk_cat.pl?cat=SSHPDG');
    if (preg_match_all("(mk_cat.pl?)", $file, $matches)) {
    echo preg_replace("(mk_cat.pl?)", "http://shop.safetynetdirect.com/cgi-bin/mk_cat.pl", $file);

    }
    else
    {
    echo $file;
    }
    ?>

    however if i add in another preg_replace underneath like this then the information from file get contents gets echod onto the page twice. any suggestions

    <?php
    $pattern = '(/cgi-bin/)';
    $replacement = '(http://shop.s)';
    $file = file_get_contents('http://shop.safetynetdirect.com/cgi-bin/mk_cat.pl?cat=SSHPDG');
    if (preg_match_all("(mk_cat.pl?)", $file, $matches)) {
    echo preg_replace("(mk_cat.pl?)", "http://shop.safetynetdirect.com/cgi-bin/mk_cat.pl", $file);
    echo preg_replace("(/cgi-bin/)", "http://shop.safetynetdirect.com/cgi-bin/", $file);
    }
    else
    {
    echo $file;
    }
     
    dean5000v, Jun 10, 2008 IP
  7. dean5000v

    dean5000v Peon

    Messages:
    201
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #27
    i just simply stored the values into a array, got it working now thanks !
     
    dean5000v, Jun 10, 2008 IP