Como marcar URLs automaticamente em um texto
Autor: Danniel | Publicado: 2017-02-02T11:11:00-08:00

Criei a classe Autourl para marcar URLs automaticamente em um texto.
Se for uma URL do youtube, será embutido o player.
Se for uma URL de imagem será exibida a imagem.
E as demais URIs serão exibidas como links.


&lt;?php

class Autourl
{
&nbsp; &nbsp; public static function isYoutube($url)
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; $pattern = '~http(s){0,1}://(www.){0,1}(youtube.com/watch\?v=|y2u.be/|youtu.be/)[a-zA-Z0-9]{1,}~';
&nbsp; &nbsp; &nbsp; &nbsp; return preg_match($pattern, $url);
&nbsp; &nbsp; }
&nbsp; &nbsp;
&nbsp; &nbsp; public static function youtubeCode($url)
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; return substr($url, strpos($url, '=') + 1);
&nbsp; &nbsp; }
&nbsp; &nbsp;
&nbsp; &nbsp; public static function processUrl($url)
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; if (Autourl::isYoutube($url))
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '&lt;iframe ' .
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'class="youtube" ' .
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'width="560" ' .
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'height="315" ' .
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'src="https://www.youtube.com/embed/' . Autourl::youtubeCode($url) . '" ' .
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'frameborder="0" ' .
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'allowfullscreen' .
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '&gt;' .
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '&lt;/iframe&gt;';
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; elseif (Autourl::isImage($url))
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '&lt;img src="' . $url . '" class="imagem" /&gt;';
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; else
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '&lt;a ' .
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'href="' . (strtoupper(substr($url, 0, 3)) == 'WWW' ? 'http://' : '') . $url . '" ' .
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'target="' . $url . '"' .
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '&gt;' .
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $url .
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '&lt;/a&gt;';
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; }
&nbsp; &nbsp;
&nbsp; &nbsp; public static function get($texto, $width = 560, $height = 315)
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; $padrao = "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&amp;@#\/%?=~_|!:,.;]*[-a-z0-9+&amp;@#\/%=~_|]/i";
&nbsp; &nbsp; &nbsp; &nbsp; return
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; preg_replace_callback
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $padrao,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; create_function
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '$matches',
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'return Autourl::processUrl($matches[0]);'
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $texto
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );
&nbsp; &nbsp; }
&nbsp; &nbsp;
&nbsp; &nbsp; public static function isImage($url)
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; $posicao_interrogacao = strpos($url, '?');
&nbsp; &nbsp; &nbsp; &nbsp; $extensoes = ['jpg', 'bmp', 'gif', 'png'];
&nbsp; &nbsp; &nbsp; &nbsp; foreach ($extensoes as $extensao)
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($posicao_extensao = stripos($url, '.' . $extensao))
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; return $posicao_extensao &amp;&amp; (!$posicao_interrogacao || $posicao_interrogacao &gt; $posicao_extensao);
&nbsp; &nbsp; }
}

$texto = 'Saúde" todas as segundas-feiras, às 9h15, com reprise às sextas-feiras, a partir das 13 horas.
Durante a exibição da atração, a apresentadora costuma receber profissionais da saúde para dar dicas de alimentação, medicação, postura e qualidade de vida aos telespectadores.
https://www.youtube.com/watch?v=tNR3lGyiLQA
http://www.pudim.com.br/pudim.jpg
Breve histórico da apresentadora: Gisela Savioli foi a nutricionista responsável pela alimentação dos
www.pudim.com.br';


echo nl2br(Autourl::get($texto));
?&gt;


O resultado será: