Geting img url and video url from a string - php

I have a string of 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' \/>
and I need only to extract the url from it.
Similarly I have this 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"}]
and need to extract only the links to the videos in src.
How do I do that?
update:
this resolved the img scr problem
$dom = new DOMDocument();
$dom->loadHTML($ava);
$url = $dom->getElementsByTagName('img')->item(0)->getAttribute('src');

Assuming you're doing this in php, to be able to extract the JSON information you want, you just simple have to decode it into a php array and then pull out the part you need. So for example:
$json = your_json_feed;
$data = json_decode($json);
$video_url_tmp = $data["post_content"];
That will make $video_url_tmp contain:
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
Which is the part of the JSON feed that you want, that has the URL. Then you'll need a RegEx to pull out the section that you need. It might be easier to RegEx "id=\"viddler-ce3d9b65\" part out, and then figure out how viddler creates its links/embeds/whatever and the form it yourself.
Hope this helps you out.
For referencejson_decode:
http://php.net/manual/en/function.json-decode.php

Related

How do I strip the url of a google news json feed link

How do I strip the url of a google news json feed link?
My link looks like this
http%253A%252F%252Fwww.boston.com%252Fbusiness%252Fnews%252F2014%252F12%252F04%252Fnasa-poised-usher-new-era-with-orion-launch%252FGOh9asOZiRJPHbNw60otUK%252Fstory.html
I have used the following code
strip_tags("".$row['url']."");
This does not do anything, I'm guessing this is possible and I am using the wrong php function.
You can use urldecode for this.
$url = "http%253A%252F%252Fwww.boston.com%252Fbusiness%252Fnews%252F2014%252F12%252F04%252Fnasa-poised-usher-new-era-with-orion-launch%252FGOh9asOZiRJPHbNw60otUK%252Fstory.html";
$url = urldecode(urldecode($url));
echo $url; // Will output: http://www.boston.com/business/news/2014/12/04/nasa-poised-usher-new-era-with-orion-launch/GOh9asOZiRJPHbNw60otUK/story.html

Google news feed content

So lets say i have a google news feed, like this: https://news.google.com/news/feeds?pz=1&cf=all&ned=no_no&hl=no&q=%22something%22&output=atom&num=1
Grabbing the title, author and link would be easy, but how would i go around getting say the first 200 characters of the content? its full of html, and mixed in with the title and author aswell.
i could use strip_tags on it, but it would still be a mess.
Any way to make google return a ['description'] maybe?
or is there perhaps any other good news feeds that gives me the content in a way thats easier to manage?
[edit]
Update on how i ended up doing it.
$news = #simplexml_load_string(file_get_contents('https://news.google.com/news/feeds?pz=1&cf=all&ned=no_no&hl=no&q=%22molde+fotballklubb%22+OR+%22tornekrattet%22+OR+%22mfk%22+OR+%22oddmund+bjerkeset%22+-%22moss%22&output=atom&num=1'), 'SimpleXMLElement', LIBXML_NOCDATA);
$data = get_object_vars($news->{'entry'});
$test = explode('<font size="-1">', $data['content']);
$link = get_object_vars($data['link']);
$return['title'] = strip_tags($test[0]);
$return['author'] = strip_tags($test[1]);
$return['description'] = strip_tags($test[2]);
$return['link'] = $link['#attributes']['href'];
It is still not working properly, but thats because the feed gives me the content in different ways all the time. Sometimes the content of the news article itself will just be metadata like the authors and image descriptions.
And the breaking it up by html tags when the html have changes from time to time causes some problems. But i cant figure out any othe way of doing it with this feed.
You could try loading the HTML in a DOMDocument instance and extract the parts you need, or use a wrapper for it like Goutte which makes it a lot easier to extract portions you need.
http://php.net/manual/en/class.domdocument.php
https://github.com/fabpot/Goutte

How can I determine if a vimeo URL is a video? PHP

So I've been using this simple method to determine whether or not a URL is from vimeo
if (preg_match("/vimeo/",$url)) {
$getvim = (parse_url($url));
$vimid = str_replace("/", "", $getvim['path']);
}
As you can see this simply determines if the url contains "vimeo" then grabs the video id by grabbing everything after the slash. Works fine if you're actually linking a vimeo video, however utterly messes everything up if the link just contains "vimeo" without actually being a vimeo link.
Does anyone have a better method of doing this? There are tons of YouTube solutions available but finding a good vimeo one has been nearly impossible. Any help with this is appreciated, thank you.
Try this code:
$urls = parse_url($url);
if ($urls['host'] == 'vimeo.com'){
$vimid = ltrim($urls['path'], '/');
if (is_numeric($vimid)) {
// do something with $vimid
}
}
We assume that all video IDs are numerical.
Read the source of the page via file_get_contents($url) and then check if it contains the string <meta property="og:video" content="http://vimeo.com/moogaloop.swf?clip_id=".$vimid." /> If if contains that string, then you know its a valid video.

How to get post URL out of the Blogger API in PHP

In Short, I am pulling the feed from my blogger using the Zend API in PHP. I need to get the URL that will link to that post in blogger. What is the order of functions I need to call to get that URL.
Right now I am pulling the data using:
$query = new Zend_Gdata_Query('http://www.blogger.com/feeds/MYID/posts/default');
$query->setParam('max-results', "1");
$feed = $gdClient->getFeed($query);
$newestPost = $feed->entry[0];
I can not for the life of me figure out where I have to go from here to get the URL. I can successfully get the Post title using: $newestPost->getTitle() and I can get the body by using $newestPost->getContent()->getText(). I have tried a lot of function calls, even ones in the documentation and most of them error out. I have printed out the entire object to look through it and I can find the data I want (so I know it is there) but the object is too complex to be able to just look at and see what I have to do to get to that data.
If anyone can help me or at least point me to a good explanation of how that Object is organized and how to get to each sub object within it, that would be greatly appreciated.
EDIT: Never mind I figured it out.
You are almost there, really all you need to do is once you have your feed entry is access the link element inside. I like pretty URLs so I went with the alternate rather than the self entry in the atom feed.
$link = $entry->link[4]->href;
where $entry is the entry that you are setting from the feed.
The solution is:
$query = new Zend_Gdata_Query('http://www.blogger.com/feeds/MyID/posts/default');
$query->setParam('max-results', "1");
$feed = $gdClient->getFeed($query);
$newestPost = $feed->entry[0];
$body = $newestPost->getContent()->getText();
$body now contains the post contents of the latest post (or entry[0]) from the feed. This is just the contents of the body of the post, not the title or any other data or formatting.

picasa link to usable cooliris link

Ok, I've got this link:
https://picasaweb.google.com/110727246651632161982/2042011#
and I want to use it for cooliris, on their website they make it like this:
picasaweb.google.com%2F%3Fuser%3D110727246651632161982%26album%3D2042011
I want users to post the normal picasa link into my website, then the link has to be replaced and pasted into my database. I know how to do that, but how to get from the picasa link to the usable cooliris link?
Example: DECODE
$url = 'picasaweb.google.com%2F%3Fuser%3D110727246651632161982%26album%3D2042011';
echo urldecode($url);
Output
picasaweb.google.com/?user=110727246651632161982&album=2042011
Example: ENCODE
$url = 'picasaweb.google.com/110727246651632161982/2042011#';
echo urlencode($url);
Output
picasaweb.google.com%2F110727246651632161982%2F2042011%23
References:
urldecode()
urlencode()

Categories