Jump to content

Excluding image and video content from recent Tumblr posts script?


Recommended Posts

I've setup a PHP script at the bottom of my homepage on this website. It will grab the three most recent text posts from my blog and display a summary. It's setup to check for updated content every hour (cache).

http://charlieswebsites.co.uk/fearofmobs/site

 

It works good, but how can I make sure that it only ever returns <p> tags? Sometimes video iframes are embedded and images at the beggining of posts, and as you can see it posts those. I only want textual content returned.

 

The script in use is:

 

<?php
$cachefile_blog = 'blogposts.txt';

$cache_timer = 3600 + @filemtime($cachefile_blog);// files timestamp + 3600 seconds

if (file_exists($cachefile_blog) && time() < $cache_timer ) {
// do nothing if file is fresh
}
else 
{

// go get latest results
$data = file_get_contents("http://blog.fearofmobs.com/api/read?type=post&type=post&num=3");

// overwrite the cached file with new data
$fh = fopen($cachefile_blog, 'w') or die("can't open file");
fwrite($fh, $data);
fclose($fh);
}

// read the cached file
$request_url = file_get_contents($cachefile_blog);
$xml = simplexml_load_string($request_url);

///simplexml_load_string --- for local files
///simplexml_load_file --- for URLS

$cpost = $_GET["post"];
$tumblename = 'fearofmobs';
if (is_numeric($cpost)){
$request_url = 'http://' . $tumblename . '.tumblr.com/api/read?id=' . $cpost;
$xml = simplexml_load_file($request_url);
if($xml ===  FALSE){
}
else {
foreach ($xml->posts->post as $post) {
}}}
else {
$request_url = 'http://' . $tumblename . '.tumblr.com/api/read?num=3&type=post';
$xml = simplexml_load_file($request_url);
foreach ($xml->posts->post as $post) {
$title = $post->{'regular-title'};
$body = $post->{'regular-body'};
$aname = $post['id'];
$tlink = 'http://' . $tumblename . '.tumblr.com/' . $post['id'];
$small_post = substr($body,0,132);
echo '<div class="post"><a href="'.$tlink.'"><h4 class="post_title">'.$title.'</h4></a>'.$small_post.'…</p><span class="post_link"><a href="'.$tlink.'">Continue reading »</a></span></div>';
}
}

?>';
}
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.