SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Thread: Preg replace

  1. #1
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Preg replace

    Im trying to replace relative links, stuff like /images/test.gif or folder/file.html with http://www.fullurl.com/restofurl. So far I have this:

    PHP Code:
    $html file_get_contents($page);
    $html preg_replace('#(href)="(\/)#is','http://www.fullurl.com/"',$html); 
    But that only replaces if the link starts with /, I need to replace anything that doesnt begin with http:// in the link.

    Thanks
    Silly

  2. #2
    SitePoint Addict pentium10's Avatar
    Join Date
    Apr 2004
    Location
    Transilvania
    Posts
    208
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why don`t you try this to put in the document head tag!

    <base href="http://www.fullurl.com/restofurl">
    Rype :: book, do, charge, collect,
    track time, track expenses and invoice easily on the go.
    Available on Mac, Win, iPhone, WinMobile.
    http://www.rypenow.com

  3. #3
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can you please one more example of the data that you are trying to replace and to what?

  4. #4
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jaswinder_rana
    can you please one more example of the data that you are trying to replace and to what?
    Im using file_get_contents to grab the html code from a webpage and put into a string variable. In the html code on the page they link using relative path and not absolute path, so the links would look like this http://localhost/pathonsite/file.html instead of http://www.somesite.com/pathonsite/file.html. I need it to look like the latter.

    Thanks
    Silly

  5. #5
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pentium10
    Why don`t you try this to put in the document head tag!

    <base href="http://www.fullurl.com/restofurl">
    Actually that worked in fixing the images and such, now I dont have to use preg_replace to give the full url. But the links on the page need to be clickable to the absolute path. If I click on a link that shows /path/file.html, it obviously wont work as it is only relative to the site itself. So I need to put the full url in front of the relative path.

    Silly

  6. #6
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry i am being dumb. need more coffee i think. BUT can you please put 2 (or more) links as they are exactly in the text file like

    somefile.txt
    the file contents go here
    well i know what you want them. its just i can't figure how they are stored in text file. i wanna have something so i can test my example on it.

    sorry for asking repeatdly but before i can i say anything i just want to know what am i going to do ?? hope you'll post again

  7. #7
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    not sure what you want exactly BUT try this
    PHP Code:
    $string 'href="path/file.php"';
    echo 
    preg_replace('~href="/?(.*)"~',"http://www.abc.com/\${1}",$string); 

  8. #8
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no problem

    I read from a webpage and put the webpage code into a string

    PHP Code:
    $html file_get_contents('http://www.somesite.com/file.html'); 
    When you echo $html you get the sites html code. In that code you will have links like this:

    <td><a href="/store/file.html"></td>

    I want to change that to this:

    <td><a href="http://www.somesite.com/store/file.html"></td>

    But it might also look like this:

    <td><a href="store/file.html"></td>

    I still need to change it to:

    <td><a href="http://www.somesite.com/store/file.html"></td>

    If the link already has http://www.somesite.com at the beginning then I dont want to touch it.

    Silly

  9. #9
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this
    PHP Code:
    $site 'http://www.abc.com';
    $string 'href="path/file.php"';
    $string 'href="'.$site.'/path/file.php"';
    echo 
    preg_replace("~href=\"($site)?/?(.*)\"~","http://www.abc.com/\${2}",$string); 
    i put two $string one with http://path and one without just to test

    EDITED: FOR CORRECTING CODE

  10. #10
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. That worked. I had to add href= to the replacement string though.

    Thanks again!
    Silly

  11. #11
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, it's fine as long as he has no links to third party sites like http://sitepoint.com

    BTW, if you grab the page for presentation purposes only, pentium's suggestion is absolutely correct and even recommended...

  12. #12
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog
    Ok, it's fine as long as he has no links to third party sites like http://sitepoint.com

    BTW, if you grab the page for presentation purposes only, pentium's suggestion is absolutely correct and even recommended...
    This project doesnt require linking to third party. It is for our customers and their products. So everything has to be on their site. Thanks again for all the replies.

    Silly

  13. #13
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just a little change to over come the limitation stereofrog mentioned
    PHP Code:
    $site[] = 'http://www.abc.com';
    $site[] = 'http://abc.com';
    $string 'href="/path/file.php"';
    $string 'href="'.$site[0].'/path/file.php"';
    $string 'href="'.$site[1].'/path/file.php"';
    $siteStr implode('|',$site);
    echo 
    preg_replace("~href=\"($siteStr)?/?(.*)\"~","http://www.abc.com/\${2}",$string); 


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
  •