I need a Preg Replace algo to replace SRC links

Discussion in 'PHP' started by redhits, Oct 9, 2007.

  1. #1
    Hello , i need an preg replace algoritm that will replace short links to full links.
    Like , it will replace /home, to website.com/home, and page.html to website.com/page.html , the problem is that on that page there can be pages with src="http:// (full url) and src="" short urls
     
    redhits, Oct 9, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    $text = preg_replace(
    	'/<a([^>]+)href="(?!(http:))/i',
    	'<a$1href="http://www.example.com/$2',
    	$text
    );
    
    PHP:
     
    nico_swd, Oct 9, 2007 IP
  3. redhits

    redhits Notable Member

    Messages:
    3,023
    Likes Received:
    277
    Best Answers:
    0
    Trophy Points:
    255
    #3
    I don't known why it's just not working
    try
    $text=file_get_contents('http://google.com');
     
    redhits, Oct 9, 2007 IP
  4. redhits

    redhits Notable Member

    Messages:
    3,023
    Likes Received:
    277
    Best Answers:
    0
    Trophy Points:
    255
    #4
    I mean it's working , but it's not replacing the images, only the URLs

    $website = preg_replace(
    '/<a([^>]+)src="(?!(http:))/i',
    '<a$1src="http://www.softgroups.com/$2',
    $website
    );


    i try this also , but without luck
     
    redhits, Oct 9, 2007 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    It works for me. :confused:

    Maybe you want to replace the old search pattern with this to make it work with https links too.
    
     '/<a([^>]+)href="(?!(https?:))/i'
    
    PHP:
    What kind of output do you get?
     
    nico_swd, Oct 9, 2007 IP
  6. redhits

    redhits Notable Member

    Messages:
    3,023
    Likes Received:
    277
    Best Answers:
    0
    Trophy Points:
    255
    #6
    no , no , i want to also have img src rewrited :)
     
    redhits, Oct 9, 2007 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    Give this a try.
    
    $text = preg_replace(
        '/\s(href|src)=["\']?\/?(?!(https?:))([^>"\'\s]+)/i',
        ' $1="http://www.example.com/$3"',
        $text
    );
    
    
    PHP:
     
    nico_swd, Oct 9, 2007 IP
  8. redhits

    redhits Notable Member

    Messages:
    3,023
    Likes Received:
    277
    Best Answers:
    0
    Trophy Points:
    255
    #8
    Wow you really good. One question
    what if I want to add after this an <img src=""> to show an visual pagerank

    what should i do ?
     
    redhits, Oct 9, 2007 IP