-1

I am trying to extract URL out of a string. The format of the string will be:

some text! some numbers http://linktoimage.com/image

I found this post earlier Extract URLs from text in PHP

and I think this solution mentioned there could work:

<?php
$string = "this is my friend's website http://example.com I think it is coll";
echo explode(' ',strstr($string,'http://'))[0]; //"prints" http://example.com

However I do not understand what it actually does. Would someone mind explaining this to me to me ?

Community
  • 1
  • 1
Tom
  • 41
  • 7
  • 1
    Have you looked up the functions you are using in the official documentation or elsewhere on the web? Contrary to popular believe Stack Overflow is not a community driven google result fetcher. – PeeHaa Jul 30 '15 at 10:07

1 Answers1

-1

You have this string:

this is my friend's website http://example.com I think it is coll

strstr($string,'http://') will return

http://example.com I think it is coll

explode(' ', ...) then will split this resulting string at the space character resulting in

array(
  0 => 'http://example.com',
  1 => 'I',
  2 => 'think',
  3 => 'it',
  4 => 'is',
  5 => 'coll'
)

and finally [0] returns the first item of this array, which is:

http://example.com

Further reading:

http://php.net/manual/en/function.strstr.php

http://php.net/manual/en/function.explode.php

t.h3ads
  • 1,858
  • 9
  • 17
  • What if url is https or written without http ? – krishna Jul 30 '15 at 10:12
  • Then it is not matched. The code you presented just does a simple string matching. If there is no `http://` in the string, it doesn't match. I could be useful to use regular expressions for that as suggested in the question you were referring to: http://stackoverflow.com/questions/910912/extract-urls-from-text-in-php – t.h3ads Jul 30 '15 at 10:15