从字符串中获取图像URL和视频URL

时间:2014-07-22 11:03:47

标签: php json wordpress

我有一串html

<img alt='' src='http:\/\/1.gravatar.com\/avatar\/9ed48ee692d84679c0aa2509985a4c74?s=96&amp;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&amp;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&lt;iframe id=\"viddler-ce3d9b65\" src=\"\/\/www.viddler.com\/embed\/ce3d9b65\/?f=1&amp;autoplay=0&amp;player=full&amp;secret=53324891&amp;disablebackwardseek=0&amp;disableseek=0&amp;disableforwardseek=0&amp;loop=0&amp;nologo=0&amp;hd=0\" width=\"437\" height=\"288\" frameborder=\"0\" mozallowfullscreen=\"true\" webkitallowfullscreen=\"true\"&gt;&lt;\/iframe&gt;\r\n\r\n&nbsp;","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');

1 个答案:

答案 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&lt;iframe id=\"viddler-ce3d9b65\" src=\"\/\/www.viddler.com\/embed\/ce3d9b65\/?f=1&amp;autoplay=0&amp;player=full&amp;secret=53324891&amp;disablebackwardseek=0&amp;disableseek=0&amp;disableforwardseek=0&amp;loop=0&amp;nologo=0&amp;hd=0\" width=\"437\" height=\"288\" frameborder=\"0\" mozallowfullscreen=\"true\" webkitallowfullscreen=\"true\"&gt;&lt;\/iframe&gt;\r\n\r\n&nbsp;

哪个是您想要的JSON Feed的一部分,它具有URL。然后,您需要一个RegEx来提取您需要的部分。 RegEx "id=\"viddler-ce3d9b65\"可能更容易解决,然后弄清楚viddler如何创建其链接/嵌入/任何形式以及它自己的形式。

希望这可以帮助你。

供参考json_decodehttp://php.net/manual/en/function.json-decode.php