How to detect a url in a text and convert it to a href

0

This function is failing to check if there is a url in the text to add a href and so if the user clicks there, redirect it

function url($text)
{   
    $text=html_entity_decode($text);
    $text=" ".$text;
    $text=preg_replace('/(http(0,1):\/\/[\w\-\.\/#?&=]*)/','<a href="$1" target="_blank">$1</a>',$text);
    return $text;
}


<div class="publi-contenido">
    <p><?php echo $posts['contenido']; ?></p>

static function agregar($CodUsua, $contenido, $img,$categoria)
{
    $textf=url($contenido);
    $con = conexion("root", "");
    $consulta = $con->prepare("insert into post(CodPost, CodUsua, contenido, img,categoria) values(null, :CodUsua, :contenido, :img, :categoria)");
    $consulta->execute(array(':CodUsua' => $CodUsua,
                             ':contenido' => $textf,
                             ':img' => $img,
                             ':categoria' => $categoria));
}
    
asked by Carlos Bonilla 08.07.2017 в 07:20
source

1 answer

0

There are cases that are not considered in the regular expression, such as if it starts with https instead of http, or does not have www.

Here is another expression you can try:

"/(http|https)\:\/\/[a-zA-z0-9\-\.]+\.[a-zA-z]{2,3}(\/\S*)?/"

I hope it works for you. This is the origin .

    
answered by 08.07.2017 в 08:05