How to add a function with a regular expression to define links and replace them so that the anchor is a domain, for example, the function below makes, for example,
http://youtube.com/video123123
-
<a href='http://youtube.com/video123123'>http://youtube.com/video123123</a>
And I need to get
<a href='http://youtube.com/video123123'>youtube.com</a>
-
preg_replace_callback( '{ (?: (\w+://) # протокол с двумя слэшами | # - или - www\. # просто начинается на www ) [\w-]+(\.[\w-]+)* # имя хоста \S* # URI (но БЕЗ кавычек) (?: # последний символ должен быть... (?<! [[:punct:]] ) # НЕ пунктуацией | (?<= [-/&+*] ) # но допустимо окончание на -/&+* ) }xis', create_function ( '$match', // Если нет протокола, добавляем его в начало строки. '$href = !empty($match[1])? $match[0] : "http://".$match[0]; // Формируем ссылку. return \'<a href="\'.$href.\'" target="_blank">\'.$match[0].\'</a>\';' ), $text )
Thank you in advance.