我有一串html
<img alt='' src='http:\/\/1.gravatar.com\/avatar\/9ed48ee692d84679c0aa2509985a4c74?s=96&d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&r=G' class='avatar avatar-96 photo' height='96' width='96' \/>
我只需要从中提取网址。
同样我有这个json
[{"ID":1,"post_author":"1","post_date":"2014-04-17 16:09:47","post_date_gmt":"2014-04-17 16:09:47","post_content":"Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!\r\n\r\n<iframe id=\"viddler-ce3d9b65\" src=\"\/\/www.viddler.com\/embed\/ce3d9b65\/?f=1&autoplay=0&player=full&secret=53324891&disablebackwardseek=0&disableseek=0&disableforwardseek=0&loop=0&nologo=0&hd=0\" width=\"437\" height=\"288\" frameborder=\"0\" mozallowfullscreen=\"true\" webkitallowfullscreen=\"true\"><\/iframe>\r\n\r\n ","post_title":"Hello world!","post_excerpt":"","post_status":"publish","comment_status":"open","ping_status":"open","post_password":"","post_name":"hello-world","to_ping":"","pinged":"","post_modified":"2014-05-09 09:35:11","post_modified_gmt":"2014-05-09 09:35:11","post_content_filtered":"","post_parent":0,"guid":"http:\/\/neevo.simple-task.com\/?p=1","menu_order":0,"post_type":"post","post_mime_type":"","comment_count":"2","filter":"raw"}]
并且只需要提取src中视频的链接。
我该怎么做?
更新: 这解决了img scr问题
$dom = new DOMDocument();
$dom->loadHTML($ava);
$url = $dom->getElementsByTagName('img')->item(0)->getAttribute('src');
答案 0 :(得分:0)
假设您在php中执行此操作,为了能够提取您想要的JSON信息,您只需简单地将其解码为php数组,然后拉出您需要的部分。例如:
$json = your_json_feed;
$data = json_decode($json);
$video_url_tmp = $data["post_content"];
这将使$video_url_tmp
包含:
Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!\r\n\r\n<iframe id=\"viddler-ce3d9b65\" src=\"\/\/www.viddler.com\/embed\/ce3d9b65\/?f=1&autoplay=0&player=full&secret=53324891&disablebackwardseek=0&disableseek=0&disableforwardseek=0&loop=0&nologo=0&hd=0\" width=\"437\" height=\"288\" frameborder=\"0\" mozallowfullscreen=\"true\" webkitallowfullscreen=\"true\"><\/iframe>\r\n\r\n
哪个是您想要的JSON Feed的一部分,它具有URL。然后,您需要一个RegEx来提取您需要的部分。 RegEx "id=\"viddler-ce3d9b65\"
可能更容易解决,然后弄清楚viddler如何创建其链接/嵌入/任何形式以及它自己的形式。
希望这可以帮助你。
供参考json_decode
:
http://php.net/manual/en/function.json-decode.php