Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fix this URL regexp to match URLs at the end of the string?

I found this great URL matching regexp from another answer here that gets URL's in a string, but it only works if they are followed by spaces.

preg_replace('#(https?|ftp)://[^ ]+ #i', '', $s['Text']);

How would I modify this so that it will also match URL's that are at the very end of a string with nothing after them?

like image 266
Michael F Avatar asked Feb 04 '26 20:02

Michael F


1 Answers

For matching with all kind of URLs the following code can help you:

<?php

$content = '<html>

<title>Random Website I am Crawling</title>

<body>

Click <a href="http://clicklink.com">here</a> for foobar

Another site is http://foobar.com';

$regex = "((https?|ftp)\:\/\/)?"; // SCHEME
$regex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?"; // User and Pass
$regex .= "([a-z0-9-.]*)\.([a-z]{2,4})"; // Host or IP
$regex .= "(\:[0-9]{2,5})?"; // Port
$regex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // Path
$regex .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; // GET Query
$regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; // Anchor


$matches = array(); //create array
$pattern = "/$regex/";

preg_match_all($pattern, $content, $matches); 

print_r(array_values(array_unique($matches[0])));
echo "<br><br>";
echo implode("<br>", array_values(array_unique($matches[0])));


?>
like image 107
Adrian Cid Almaguer Avatar answered Feb 06 '26 08:02

Adrian Cid Almaguer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!