开发者

Extracting URLs from a JSON-like string

I need to extract the first URL from some content. The content may be like this:

({items:[{url:"http://cincinnati.ebayclassifieds.com/",name:"Cincinnati"},{url:"http://dayton.ebayclassifieds.com/",name:"Dayton"}],error:null}); 

or may contain only a link

({items:[{url:"http://portlandor.ebayclassifieds.com/",name:"Portland (OR)"}],error:null}); 

currently I have :

$pattern = "/\:\[\{url\:\"(.*)\"\,name/";
preg_match_all($pattern, $htmlContent, $matches);
$URL = $matches[1][0];

however it works only if there is a 开发者_StackOverflow中文版single link so I need a regex which should work for the both cases.


You can use this REGEX:

$pattern = "/url\:\"([^\"]+)\"/";

Worked for me :)


Hopefully this should work for you

<?php
$str = '({items:[{url:"http://cincinnati.ebayclassifieds.com/",name:"Cincinnati"},{url:"http://dayton.ebayclassifieds.com/",name:"Dayton"}],error:null});'; //The string you want to extract the 1st URL from

$match = ""; //Define the match variable
preg_match("%(((ht|f)tp(s?))\://)?(www.|[a-zA-Z].)[a-zA-Z0-9\-\.]+\.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\;\?\'\\\+&amp;\%\$#\=~_\-]+))*%",$str,$match); //I Googled for the best Regular expression for URLs and found the one included in the preg_match

echo $match[0]; //Return the first item in the array (the first URL returned)
?>

This is the website that I found the regular expression on: http://regexlib.com/Search.aspx?k=URL

like the others have said, json_decode should work for you aswell


That smells like JSON to me. Try using http://php.net/json_decode


Looks like JSON to me, visit http://php.net/manual/en/book.json.php and use json_decode().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜