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

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

<?php
$text =  "This is the string texts from: http://test.com/test.php  ";
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;
?>

Cheers
0
 
GkCommented:
use string operations to search or do regular expression matching.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
you have a couple of methods, starting with parse_url:
http://php.net/manual/en/function.parse-url.php

0
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:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_26963940.html
$string = "This is the string texts from: http://test.com/test.php";
$strArray = explode(' ', $string);
foreach($strArray as $word)
{
	if (substr($word, 0, 7) == "http://" || substr($word, 0, 8) == "https://")
	{
		$url = $word;
		break;
	}
}

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

Open in new window

0
 
Loganathan NatarajanLAMP DeveloperAuthor Commented:
thanks, this works perfect for dynamically change text and url...
0
 
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 ..got it
0
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.