Hello HashCode! Tell me how you can turn the link into text: http://google.com on

<a href = "http://google.com"> http://google.com </a> 

I tried using str_replace (text); but the fact is that in addition to the link, there may be just a text.

  • preg_replace? make your own expression ... - zb '

3 answers 3

 $переменная_с_текстом = preg_replace("#(https?|ftp)://\S+[^\s.,> )\];'\"!?]#",'<a href="\\0">\\0</a>',$переменная_с_текстом); 
  • Thanks for the answer, but can you tell me what the character set is # (https? | Ftp): // \ S + [^ \ s.,>)]; '\ "!?] # - Angus123
  • This is a regular expression . Wikipedia.org/wiki/… - johniek
  • Links like www.google.com, google.ru will not recognize. And then the meaning of this regular expression? - ModaL
  • @Modal and who in TZ wrote that it is necessary to recognize also domains? there is only about http: // written - zb '
  • @Johniek and who said that commas are not allowed in url? - zb '

I think it would be better:

$site = "http://google.com"; $pattern = "/^((https?|ftp)\:\/\/)?([a-z0-9]{1})((\.[a-z0-9-])|([a-z0-9-]))*\.([az]{2,6})(\/?)$/"; $replace = "<a href=\"\\0\">\\0</a>"; $result = preg_replace($pattern, $replace, $site); echo $result;

    It's so simple that it’s already funny

    When you enter the line "Hi this my site: site.net", it displays "Hi this my site: site.net ";


     function s_link ($text){ $arrt = array(); $arrt = explode(" ", $text); $dot; for($i=0;$i<count($arrt);$i++){ if(strpos($arrt[$i],".")){ $dot .= "<a href='http://".$arrt[$i]."'>".$arrt[$i]."</a> "; }else{ $dot .= $arrt[$i]." "; } } return $dot; } 
    • And about the "just text" how to be? - tutankhamun