[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 858
  • Last Modified:

PHP extract url from string

Hello,

I am having a problem with some code I am using, here is my problem:

I have a string $string = "Some Text here and a http://www.youtube.com/watch?v=8OE-2JHcTOw and some more text here";

I want to pull the URL out of the string and put it into it's own variable called $url and
I want to have the rest of the remaining text put into a new variable called $newString

Any ideas on how to do this?? I am completely stumped (I know Regex may have something to do with this??)

Thanks In Advance,
George
0
piixeldesigns
Asked:
piixeldesigns
  • 5
  • 4
  • 2
1 Solution
 
Terry WoodsIT GuruCommented:
$string = "Some Text here and a http://www.youtube.com/watch?v=8OE-2JHcTOw and some more text h
ere";
$pattern = "/(.*?)(http:\S+)(.*)/is";
if (preg_match($pattern, $string, $matches)) {
  print_r($matches);
  $url = $matches[2];
  $newString = $matches[1].$matches[3];
  print "\$url: $url\n";
  print "\$newString: $newString\n";
} else {
  print "No matches found\n";
}

Result:
Array
(
    [0] => Some Text here and a http://www.youtube.com/watch?v=8OE-2JHcTOw and some more text h
ere
    [1] => Some Text here and a
    [2] => http://www.youtube.com/watch?v=8OE-2JHcTOw
    [3] =>  and some more text here
)
$url: http://www.youtube.com/watch?v=8OE-2JHcTOw
$newString: Some Text here and a  and some more text here
0
 
piixeldesignsAuthor Commented:
Thanks I will try this but sometimes the string will be different each time for example the url can be at the end or the start or in the middle (it's basically like a facebook post) people can type anything in any order.

Will this work for those variations or any variation.

Cheers,
0
 
Terry WoodsIT GuruCommented:
Yes, it should work for variations, though for now I've assumed the URL starts with http: and is following by a space.

To resolve that, http: in the pattern could be swapped with (?:https?|ftp): like this:
$pattern = "/(.*?)((?:https?|ftp):\S+)(.*)/is";
0
Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
Terry WoodsIT GuruCommented:
(I'm still assuming the URL will be followed by a space)
0
 
Terry WoodsIT GuruCommented:
If there's no guarantee of a space at the end, then we either need some other criteria to use, or we can use a pattern that matches valid url's, like some of the ones given here:
http://mathiasbynens.be/demo/url-regex
0
 
Mohamed AbowardaSoftware EngineerCommented:
0
 
Mohamed AbowardaSoftware EngineerCommented:
Please ignore the above post.

Here is your question solution:
$string = "Some Text here and a http://www.youtube.com/watch?v=8OE-2JHcTOw and some more text here";
$strArray = explode(' ', $string);
foreach($strArray as $word)
{
	if (substr($word, 0, 7) == "http://" || substr($word, 0, 8) == "https://")
	{
		$url = $word;
	} else {
		if ($newString != '')
			$newString .= ' ';
		$newString .= $word;
	}
}

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

Open in new window

0
 
piixeldesignsAuthor Commented:
Thanks Medo,

It worked perfectly however is it possible to pull out the string of words (not url) as showing it once as when it is a foreach it just adds the same content to any new posts etc...

Thanks Again
0
 
piixeldesignsAuthor Commented:
BUMP - Anyone have any ideas on this??
This is the last point to solve this issue.

Thanks Guys,
George
0
 
piixeldesignsAuthor Commented:
Thanks Again for the solution this has been fixed now.
0
 
Terry WoodsIT GuruCommented:
Was there a problem with my solution? It seemed to work perfectly.
0

Featured Post

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.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now