[HELP] regex problem

Discussion in 'PHP' started by 123extra, Oct 23, 2010.

  1. #1
    need you help here..

    I want to turn this :
    into :
    i have manage it by using this regex :
    preg_replace("#((http|https|ftp)://(\S*?\.\S*?))(\s|\;|\)|\]|\[|\{|\}|,|\"|'|:|\<|$|\.\s)#ie", "'<a href=\"$1\" target=\"_blank\">$1</a>$4'", $text);
    
    PHP:
    the problem is its not just replacing the text-url but also replace the the image-url, example
    into :
    need your help please :(
     
    123extra, Oct 23, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    $text = preg_replace('#((?:ftp|ht{2}p(?:s)?)://[^"\'<> ]+)#i', '<a href="$1">$1</a>', $text);
    PHP:
     
    danx10, Oct 23, 2010 IP
  3. 123extra

    123extra Peon

    Messages:
    1,502
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks for the reply, but its still does work. i have fixed using this function:
    function livelinked ($text){
            preg_match_all("#((http|https|ftp)://(\S*?\.\S*?))(\s|\;|\)|\]|\[|\{|\}|,|\"|'|:|\<|$|\.\s)|^(jpg)#ie", $text, $ccs);
            foreach ($ccs[3] as $cc) {
               if (strpos($cc,"jpg")==false  && strpos($cc,"gif")==false && strpos($cc,"png")==false ) {
                  $old[] = "http://".$cc;
                  $new[] = '<a href="http://'.$cc.'" target="_blank">'.$cc.'</a>';
               }
            }
            return str_replace($old,$new,$text);
    }
    PHP:
     
    123extra, Oct 23, 2010 IP