I have the following string:
"http://add.co" id="num_1"
How can I get only "http://add.co" from this string?
I tried to use pattern:
^http:\/\/(+s)*
You could use this regex ^"http:\/\/[^"]+(?=") which almost captures your url.
String : "http://add.co" id="num_1"
Matches : "http://add.co
You could append a last " to the match to fix it. Maybe someone can edit my regex to include the last ".
See example here: https://regex101.com/r/oppeaQ/1
There are a couple of ways to achieve what you want:
With preg_match:
$str = '"http://add.co" id="num_1"';
preg_match('/^"(.*?)"/', $str, $matches);
echo $matches[1];
With str_replace and explode:
$str = '"http://add.co" id="num_1"';
$url = str_replace("\"", "", explode(" ", $str)[0]);
echo $url;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With