I tried the following code to get only the first url from the string. But it not working.
The print_r($matches[0]);
is showing array result. but the echo $matches[0];
is displaying nothing, what went wrong here ?
I just want the "https://www.amazon.in/b?node=21021782031&pf_rd_r=Z8266H8XJMQGEZMB0X44&pf_rd_p=00f7186e-02c5-40c7-9f15-9f2d65d70297" to be displayed and remove all other text and links.
<?php
$string = "Samsung Galaxy S20 (Black) https://www.amazon.in/b?node=21021782031&pf_rd_r=Z8266H8XJMQGEZMB0X44&pf_rd_p=00f7186e-02c5-40c7-9f15-9f2d65d70297 afdsfdsf d https://stackoverflow.com/questions/35433139/php-get-only-values-from-an-array and https://mail.google.com/mail/u/0/#inbox";
$regex = '#https?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#';
preg_match_all($regex, $string, $matches);
echo "<pre>";
print_r($matches[0]);
echo "</br>";
echo $matches[0];
?>
the result should output only the first link from $string
like following
https://www.amazon.in/b?node=21021782031&pf_rd_r=Z8266H8XJMQGEZMB0X44&pf_rd_p=00f7186e-02c5-40c7-9f15-9f2d65d70297