Nasıl bir URL metin bir bağlantı yapabilirim?

2 Cevap

Ben "www.google.com" yazıp gönderdiğiniz zaman, bu bir bağlantı olur, Facebook ya da Twitter üzerinde söylüyorlar. PHP nasıl bu kod? Ben nerede www başlar ve. Com uçlarını almak için normal ifadeleri kullanıyor musunuz?

Bu onlar bunu nasıl mı?

<?PHP 
//some regular expression to get www and .com part
$link="<a href='$url'>$url</a>";
echo $link;
?>

Nasıl ben "www" ve ". Com" kısmını almak için normal bir ifade yazabilirim?

And for twitter's @obama, obama would become a link to obama's site. What regular expression do they use to get the text after the @ and before the space?

2 Cevap

<?php

$str = "Lorem http://myyn.org dolor sit amet, http://google.com adipisicing ..";

$str = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is",
                    "\\1<a href=\"\\2\">\\2</a>", 
                    $str); 

echo $str . "\n";

?>

Örnek:

$ php 2935574.php 
Lorem <a href="http://myyn.org">http://myyn.org</a> dolor sit amet, \
<a href="http://google.com">http://google.com</a> adipisicing elit.

Ve Twitter için böyle bir şey kullanabilirsiniz.

$str = "@obama This is a test";
$str = preg_replace('/@([\w-]+)/', '@<a href="http://twitter.com/\\1">\\1</a>', $str);
echo $str; // @<a href="http://twitter.com/obama">obama</a> this is a test