Bir Metindeki Adresleri Linke Çevirmek

Genel 1 Yorum »

Bir arkadaşın ihtiyacı için bulup buluşturduğum bir kod parçası

Kullanılan regexp cümleciği

1
@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@

Örnek kod

1
2
3
4
5
6
$string='deneme olarak http://sayfa.site.com/goster/icerik.html
bu linki ve http://sayfa.site.com/sakla/icerik.html sadece
yazı içinden alıp link yapacak.';
 
$string = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', 
'<a href="$1">$1</a>', $string);

Örnek kodun çıktısı

1
2
3
deneme olarak <a href="http://sayfa.site.com/goster/icerik.html">http://sayfa.site.com/goster/icerik.html</a>
bu linki ve <a href="http://sayfa.site.com/sakla/icerik.html">http://sayfa.site.com/sakla/icerik.html</a> sadece
yazı içinden alıp link yapacak.
WP Theme & Icons by N.Design Studio
Girdi Beslemesi Yorum Beslemesi GiriÅŸ