How to detect and remove any url from string using php

Sometimes, We have to remove any url from a string to display, for example, you have got a content string from a pdf file, however, this string contains some urls, you have to remove them before displaying on you site.

Consider a string from a pdf file:

“M A N N I N G Mark Fisher Jonas Partner Marius Bogoevici Iwein Fuld FOREWORD BY Rod Johnson IN ACTION https://www.freeaibook.com Spring Integration in Action www.freeaibook.com www.exampleinsight.com Spring Integration in Action MARK FISHER JONAS PARTNER MARIUS BOGOEVICI”.

Here are three website urls:

https://www.freeaibook.com
www.freeaibook.com
www.exampleinsight.com

You should detect and remove them from this string.

How to remove them?

Here we write a function example using php, you can follow it.

function removeURLS($str=""){

$tag='@(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))@';

preg_match_all($tag,$str,$r);

//print_r($r);

if(isset($r[1])){

$urls=$r[1];

$len=count($urls);

for($i=0;$i<$len;$i++){

$u=$urls[$i];

if($u!=""){

$str=str_ireplace($u,'',$str);

}

}

}

return $str;

}

,