Extract URL's from a string using PHP
REGEX is the answer for your problem. Taking the Answer of Object Manipulator.. all it's missing is to exclude "commas", so you can try this code that excludes them and gives 3 separated URL's as output:
$string = "The text you want to filter goes here. http://google.com, https://www.youtube.com/watch?v=K_m7NEDMrV0,https://instagram.com/hellow/";
preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $string, $match);
echo "<pre>";
print_r($match[0]);
echo "</pre>";
and the output is
Array
(
[0] => http://google.com
[1] => https://www.youtube.com/watch?v=K_m7NEDMrV0
[2] => https://instagram.com/hellow/
)
PHP regex extract url with pattern from string
You can repeat all the allowed characters before and after matching /products/
using the same optional character class. As the character class is quite long, you could shorten the notation by wrapping it in a capture group and recurse the first subpattern as (?1)
Note that you don't have to escape the forward slash using a different separator.
$re = '`\b(?:(?:https?|ftp)://|www\.)([-a-z0-9+&@#/%?=~_|!:,.;]*)/products/(?1)[-a-z0-9+&@#/%=~_|]`';
$str = <<<EOF
http://example.com/products/1/abc
This string is valid - http://example.com/products/1
This string is not valid - http://example.com/order/1
EOF;
preg_match_all($re, $str, $matches);
print_r($matches[0]);
Output
Array
(
[0] => http://example.com/products/1/abc
[1] => http://example.com/products/1
)
Extract URL from string
John Gruber has spent a fair amount of time perfecting the "one regex to rule them all" for link detection. Using preg_replace()
as mentioned in the other answers, using the following regex should be one of the most accurate, if not the most accurate, method for detecting a link:
(?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`!()\[\]{};:'".,<>?«»“”‘’]))
If you only wanted to match HTTP/HTTPS:
(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))
Related Topics
Yii2 Global Filter/Behavior to Force User to Authenticate First
Printing a New Line in a CSV File Cell
Check If a Remote Page Exists Using PHP
Php: How to Get a List of Classes That Implement Certain Interface
Zf2 + Doctrine 2 - Child-Level Discriminators with Class Table Inheritance
Strip PHP Variable, Replace White Spaces with Dashes
Creating New Laravel Project via Composer Fails with Error Class Arrayloader Not Found
How to Always Use Ignore-Platform-Reqs Flag When Running Composer
MySQL Transactions Within Transactions
Convert Epoch Time to Date PHP
JSON_Encode Produce JSON_Error_Utf8 from Mssql-Select
Best Practice for Working with Currency Values in PHP
How to Manually Return or Throw a Validation Error/Exception in Laravel
Htaccess Access to File by Ip Range
Add a Checkout Checkbox Field That Enable a Percentage Fee in Woocommerce