We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

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

Loganathan Natarajan
on
Medium Priority
275 Views
Last Modified: 2012-05-11
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" ...
Comment
Watch Question

Gk

Commented:
use string operations to search or do regular expression matching.
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
you have a couple of methods, starting with parse_url:
http://php.net/manual/en/function.parse-url.php

Freelancer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Commented:
I have answered this question here:
https://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

Loganathan NatarajanLAMP Developer
CERTIFIED EXPERT

Author

Commented:
thanks, this works perfect for dynamically change text and url...
Loganathan NatarajanLAMP Developer
CERTIFIED EXPERT

Author

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
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.