preg_match_all - regex to find full urls in string

人盡茶涼 提交于 2019-12-03 08:44:00

Well. Finally I got it:

The final regex code is:

$regex = "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i";

It works.

Check these pattern which can be used for any URL type

$regex = "((https?|ftp)\:\/\/)?"; // Checking scheme 
$regex .= "([a-z0-9-.]*)\.([a-z]{2,3})"; // Checking host name and/or IP
$regex .= "(\:[0-9]{2,5})?"; // Check it it has port number
$regex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // The real path
$regex .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; // Check the query string params
$regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; // Check anchors if are used.

You can ignore any section which you may not need. As you see I am concatenating them

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!