Wie verknüpfe ich URLs in einer Zeichenfolge mit PHP?

Ich habe folgende Zeichenfolge:

"Look on http://www.google.com". 

Ich muss es konvertieren zu:

“Schau auf http://www.google.com ”

Die ursprüngliche Zeichenfolge kann mehr als eine URL-Zeichenfolge enthalten.

Wie mache ich das in PHP?

Vielen Dank

Sie können Folgendes verwenden:

 $string = "Look on http://www.google.com"; $string = preg_replace( "~[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]~", "\\https://stackoverflow.com/questions/507436/how-do-i-linkify-urls-in-a-string-with-php/0", $string); 

PHP-Versionen <5.3 (ereg_replace) ansonsten (preg_replace)

lib_autolink macht einen ziemlich guten Job und vermeidet Fallstricke wie zusätzliche Interpunktion nach dem Link und Links in HTML-Tags:

https://github.com/iamcal/lib_autolink

Sehen Sie sich die regulären Ausdrücke an . Sie würden dann etwas tun wie :

 $text = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '○○@https://stackoverflow.com/questions/507436/how-do-i-linkify-urls-in-a-string-with-php/0$@○○', $text); 

Sie müssen reguläre Ausdrücke verwenden …

So etwas wird helfen.

 $result = preg_replace('/\b(https?|ftp|file):\/\/[-A-Zhttps://stackoverflow.com/questions/507436/how-do-i-linkify-urls-in-a-string-with-php/0-9+&@#\/%?=~_|!:,.;]*[A-Zhttps://stackoverflow.com/questions/507436/how-do-i-linkify-urls-in-a-string-with-php/0-9+&@#\/%=~_|]/i', '\https://stackoverflow.com/questions/507436/how-do-i-linkify-urls-in-a-string-with-php/0', $text); 

Versuche dies…

 < ? function link_it($text) { $text= preg_replace("/(^|[\n ])([\w]*?)([\w]*?:\/\/[\w]+[^ \,\"\n\r\t<]*)/is", "○○@https://stackoverflow.com/questions/507436/how-do-i-linkify-urls-in-a-string-with-php/0$@○○$2$3", $text); $text= preg_replace("/(^|[\n ])([\w]*?)((www)\.[^ \,\"\t\n\r< ]*)/is", "○○@https://stackoverflow.com/questions/507436/how-do-i-linkify-urls-in-a-string-with-php/0$@○○$2$3", $text); $text= preg_replace("/(^|[\n ])([\w]*?)((ftp)\.[^ \,\"\t\n\r< ]*)/is", "○○@https://stackoverflow.com/questions/507436/how-do-i-linkify-urls-in-a-string-with-php/0$@○○$2$3", $text); $text= preg_replace("/(^|[\n ])([a-zhttps://stackoverflow.com/questions/507436/how-do-i-linkify-urls-in-a-string-with-php/0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+)+)/i", "○○@https://stackoverflow.com/questions/507436/how-do-i-linkify-urls-in-a-string-with-php/0$@○○$2@$3", $text); return($text); } $text = "ini link gue: http://sapua.com 
https://sapua.com
anything1://www.sss.com
dua www.google.com
tiga http://www.google.com
ftp.sapua.com
someone@sapua.com "; print link_it($text); ?>

Prüfe meine linkify-function, die preg_replace_callback verwendet (nur PHP 5.3). Es unterstützt http, email und twitter:

http://www.jasny.net/articles/linkify-turning-urls-into-clickable-links-in-php/

 /** * Turn all URLs in clickable links. * * @param string $value * @param array $protocols http/https, ftp, mail, twitter * @param array $attributes * @param string $mode normal or all * @return string */ function linkify($value, $protocols = array('http', 'mail'), array $attributes = array(), $mode = 'normal') { // Link attributes $attr = ''; foreach ($attributes as $key => $val) { $attr = ' ' . $key . '="' . htmlentities($val) . '"'; } $links = array(); // Extract existing links and tags $value = preg_replace_callback('~(.*?|< .*?>)~i', function ($match) use (&$links) { return '< ' . array_push($links, $match[1]) . '>'; }, $value); // Extract text links for each protocol foreach ((array)$protocols as $protocol) { switch ($protocol) { case 'http': case 'https': $value = preg_replace_callback($mode != 'all' ? '~(?:(https?)://([^\s< ]+)|(www\.[^\s<]+?\.[^\s<]+))(?' . $link . '') . '>'; }, $value); break; case 'mail': $value = preg_replace_callback('~([^\s< ]+?@[^\s<]+?\.[^\s<]+)(?' . $match[1] . '') . '>'; }, $value); break; case 'twitter': $value = preg_replace_callback('~(?< !\w)[@#](\w++)~', function ($match) use (&$links, $attr) { return '<' . array_push($links, '' . $match[https://stackoverflow.com/questions/507436/how-do-i-linkify-urls-in-a-string-with-php/0] . '') . '>'; }, $value); break; default: $value = preg_replace_callback($mode != 'all' ? '~' . preg_quote($protocol, '~') . '://([^\s< ]+?)(?' . $match[1] . '') . '>'; }, $value); break; } } // Insert all link return preg_replace_callback('/< (\d+)>/', function ($match) use (&$links) { return $links[$match[1] - 1]; }, $value); }