How do I get a URL in a string of input using PHP

I have string like this,  "This is the string texts from:  " .. how do i get only the URL from this string as "" ...
LVL 36
Loganathan NatarajanLAMP DeveloperAsked:
Who is Participating?
Marco GasiConnect With a Mentor FreelancerCommented:
This works fine:

$text =  "This is the string texts from:  ";
preg_match('/\b((?#protocol)https?|ftp):\/\/((?#domain)[-A-Z0-9.]+)((?#file)\/[-A-Z0-9+&@#\/%=~_|!:,.;]*)?((?#parameters)\?[A-Z0-9+&@#\/%=~_|!:,.;]*)?/i', $text, $match);
$url = $match[0];
echo $url;

use string operations to search or do regular expression matching.
Guy Hengel [angelIII / a3]Billing EngineerCommented:
you have a couple of methods, starting with parse_url:

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Mohamed AbowardaSoftware EngineerCommented:
I have answered this question here:
$string = "This is the string texts from:";
$strArray = explode(' ', $string);
foreach($strArray as $word)
	if (substr($word, 0, 7) == "http://" || substr($word, 0, 8) == "https://")
		$url = $word;

echo "Your URL: $url<br />";

Open in new window

Loganathan NatarajanLAMP DeveloperAuthor Commented:
thanks, this works perfect for dynamically change text and url...
Loganathan NatarajanLAMP DeveloperAuthor Commented:
@ Medo3337

tried already like that , the prb is we can't fix the location or separator .. it may vary .. i was looking preg_match it
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.