S
S
Sergey Valitov2015-09-30 12:30:26
PHP
Sergey Valitov, 2015-09-30 12:30:26

How to wrap all URLs with "a" tag using regular expression in PHP?

Hello! The task is how to wrap all links in a tag using regular expressions?

<a href="адрес самой ссылки">адрес самой ссылки</a>

For http, https, for www, but by no means for iframe. iframe should be bypassed..

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Kim, 2015-09-30
@kimono

$text = 'Простые ссылки на сайт site.ru, на www.domain.com и на http://www.pupkin.net/';
$text = preg_replace('/((https?:\/\/)?(www)?([a-z0-9]+-?)?[a-z0-9]+\.[a-z]+)/ui', '<a href="$1">$1</a>', $text);
echo $text;

G
GreatRash, 2015-09-30
@GreatRash

/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w][email protected])?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w][email protected])[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[.\!\/\\w]*))?)/ig

(c) https://regex101.com/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question