Tag: php
SEO Friendly PHP Twitter Script
by Mike on Jan.25, 2010, under Web Development
There are loads of scripts out there for pulling in your Twitter posts. A lot, if not most of them, base themselves heavily on JavaScript. This is bad for 3 reasons:
- People with JS disabled can’t read them
- It’s bad for your website’s search engine rank (SEO) as the search bots can’t read them
- It slows user’s browsers down (if only slightly)
I have written a very simple PHP script that pulls in your Twitter posts, and outputs them in an unordered list. Feel free to use however you like:
<?php
// Your Twitter username
$username = 'mikemike86';
$rssUrl = "http://twitter.com/statuses/user_timeline/$username.rss";
$rss = @file_get_contents($rssUrl);
if($rss){
$xml = @simplexml_load_string($rss);
if($xml !== false){
$var=true;
foreach($xml->channel->item as $tweet){
//echo $tweet->pubDate."\n";
echo ''.substr($tweet->description,(strlen($username)+2))."\n\n";
if($var){
break;
}
}
} else {
echo "Error: RSS file not valid!";
}
} else {
echo "Error: RSS file not found. Username invalid or requires authentication";
}
?>
PHP json_encode Alternative
by Mike on Jan.25, 2010, under Web Development
The amount of times I’ve been working on a project and the environment hasn’t had the useful json_encode function installed due to PHP being < version 5.2 is quite worrying. So, I decided to write my own replacement. How very decent of me. You can use it as you will. I wrote this a long time ago, but I've noticed it's somehow wormed its way onto the php.net site under someone elses name, and someone else seems to be taking credit for it. Oh well...
<?php
if (!function_exists(’json_encode’))
{
function json_encode($a=false)
{
// Some basic debugging to ensure we have something returned
if (is_null($a)) return ‘null’;
if ($a === false) return ‘false’;
if ($a === true) return ‘true’;
if (is_scalar($a))
{
if (is_float($a))
{
// Always use “.” for floats.
return floatval(str_replace(”,”, “.”, strval($a)));
}
if (is_string($a))
{
static $jsonReplaces = array(array(”\\”, “/”, “\n”, “\t”, “\r”, “\b”, “\f”, ‘”‘), array(’\\\\’, ‘\\/’, ‘\\n’, ‘\\t’, ‘\\r’, ‘\\b’, ‘\\f’, ‘\”‘));
return ‘”‘ . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . ‘”‘;
}
else
return $a;
}
$isList = true;
for ($i = 0, reset($a); $i {
if (key($a) !== $i)
{
$isList = false;
break;
}
}
$result = array();
if ($isList)
{
foreach ($a as $v) $result[] = json_encode($v);
return ‘[' . join(',', $result) . ']‘;
}
else
{
foreach ($a as $k => $v) $result[] = json_encode($k).’:’.json_encode($v);
return ‘{’ . join(’,', $result) . ‘}’;
}
}
}
?>
An Interesting Problem
by Mike on May.07, 2009, under Web Development
Myself and another developer have begun work on a new project that will integrate into Twitter. It’s quite a novel idea and I’m very surprised it hasn’t been done already, but there will be more on that once it’s ready for public use.
But enough on that, this post is about a problem we came across when building it. Basically we wanted to create a block of 100 cells, each numbered, but the rows are to go in a zigzag fashion, starting in the bottom left at 0 and ending in top left with 100, and obviously this isn’t to be done manually. Find an example below:
100 99 98 97 96 95 94 93 92 91 81 82 83 84 85 86 87 88 89 90 80 79 78 77 76 75 74 73 72 71 61 62 63 64 65 66 67 68 69 70 60 59 58 57 56 55 54 53 52 51 41 42 43 44 45 46 47 48 49 50 40 39 38 37 36 35 34 33 32 31 21 22 23 24 25 26 27 28 29 30 20 19 18 17 16 15 14 13 12 11 1 2 3 4 5 6 7 8 9 10
Here is what I came up with (PHP):
$i=99;
for ($x = 100; $x > 0; $x--)
{
echo $x . ' ';
if($i%10==0) {
// Display new line
echo "\n";
// Loop through 10
for($y = ($x-10); $y < ($x); $y++){
echo $y . ' ';
if($y%10==0) echo "\n";
}
// Add ten onto $x and $i
$x = $x-10;
$i = $i-10;
}
$i--;
}
Although this is relatively efficient, I'm sure there must be a better way of doing it. Any ideas? Leave a comment.