0

I want to get URL only from an i have tried a thing, But it works with Simple files When i place code into my actual wordpress page, It givess error..

<?php
$string = "this is my friend's website <a href=http://facebook.com/baberzaman > I think it is coll</a>";
$url = explode(' ',strstr($string,'http://'))[0];
?>
<br><br>
<?php echo $url; 
?>

It works good. but when i put it in wordpress. It give some '[' error in this line..

$url = explode(' ',strstr($string,'http://'))[0];
    ?>

I hope to get help soon

Thanks..

  • what is the version of php – Afsar May 07 '15 at 06:07
  • I find it amusing that this and http://stackoverflow.com/questions/30091297/python-3-why-would-you-use-urlparse-urlsplit has come up around the same time. Cause this guy could really do with a php version of this module :P – Shadow May 07 '15 at 06:35

2 Answers2

0
function()[index] 

is only available since php5 so. check out your php-version. you could stilldo

$urlarray = explode(' ',strstr($string,'http://'));
$url = $urlarray[0];
Alexis Peters
  • 1,583
  • 1
  • 10
  • 17
0

I think Array dereferencing is not possible in your current version of php

your php version should be above 5.4. check this for detail explanation

For more info check out this.

so you can use like this ,if you dont want to update to latest version

$url = explode(' ',strstr($string,'http://'));
echo $url[0];

and if your string does not have http:// then you can use this regex to extract value of href.

$pattern = '~\bhref\s*+=\s*+["\']?+\K(?!#)[^\s"\'>]++~';
preg_match_all($pattern, $string, $out);
echo $out[0];
Community
  • 1
  • 1
Afsar
  • 3,104
  • 2
  • 25
  • 35
  • Your this code worked. but one issue in this.. The String with URL came ( Good Thing) And it gave me out put (http://progsofts.com/remilamande/photo/good-picture/">Good) But i want Out put only link. (http://progsofts.com/remilamande/photo/good-picture/). this is extra ">Good Now please help me to solve this.. @PeterSmith – M.BABER Zaman May 07 '15 at 06:55
  • @peter Your this code worked. but one issue in this.. The String with URL came ( Good Thing) And it gave me out put (progsofts.com/remilamande/photo/good-picture/">Good) But i want Out put only link. (progsofts.com/remilamande/photo/good-picture). this is extra ">Good Now please help me to solve this.. – M.BABER Zaman May 07 '15 at 08:34