• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

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" ...
0
Loganathan Natarajan
Asked:
Loganathan Natarajan
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Marco GasiFreelancerCommented:
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
 
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now