YouTube Width and Height Ratio
I recently needed to automatically resize embedded YouTube videos on a website, and needed to get the height of the player from the specified width. The ratio is a bit odd, and some pixels were needed to be added for control bar.
The code to calculate the height, based on a given width is as follows:
<?php
$w = '300';
$h = round((($w/100)*56.24)+25);
?>
A Better Way to Round Up to the Nearest X in PHP
A few months back I posted a little snippet to Round and Round Up to the Nearest X in PHP and I have recently noticed a minor flaw in it. If the number is already on the nearest X then it will still round up, which we don’t want it to do. So here is the updated function:
<?php
function roundUpToAny($n,$x=5) {
return $n%$x!=0 ? round(($n+$x/2)/$x)*$x : $n;
}
echo roundUpToAny(250,50);
?>
This will of course output:
250
I’ve added a simple check to see if the number requires rounding, and if it doesn’t it will just return the number unchanged.
Password Strength in PHP
A super simple (but not amazingly accurate) method of calculating the strength of a password. Basically the higher the number the stronger the password it is. If you were to use this to display a graph, then I would suggest comparing the strength of the inputted password to a known quality password.
<?php
function passwordStrength($pass,$n = 94) {
return count(array_unique(str_split($pass)))*log($n,2);
}
echo passwordStrength("thisismypassword");
?>
If you wanted to work it out as a percentage to output text (very strong, good, OK etc.) you can do it as follows:
<?php
function passwordStrength($pass,$n = 94) {
return count(array_unique(str_split($pass)))*log($n,2);
}
$inputted_password = 'password481923';
$percentage = (passwordStrength($inputted_password)/passwordStrength('jSbr32Ina7tj*10rn'))*100;
switch($percentage) {
case $percentage > 100:
echo "Very Strong";
break;
case $percentage > 75:
echo "Good";
break;
case $percentage > 50:
echo "OK";
break;
case $percentage > 25:
echo "Bad";
break;
default:
echo "Terrible!";
break;
}
?>
Output:
Good
Grab Your FeedBurner Stats Via PHP
Just a nice simple snippet to extract your current subscriber count for your feedburner feed. Just change the $feed_uri to your own slug.
<?php
$feed_uri = 'mashable';
$ch = curl_init('http://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=' . $feed_uri);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
$output = curl_exec($ch);
curl_close($ch);
$data = (object) simplexml_load_string($output,'SimpleXMLElement',LIBXML_NOCDATA);
echo 'Subscribers: ' . $data->feed->entry->attributes()->circulation;
?>
This will output:
Subscribers: 331804
If you wanted to show whether you’ve had an increase in subscribes since the day before then you can use the following snippet to extract the data.
<?php
$feed_uri = 'mashable';
$ch = curl_init('http://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=' . $feed_uri . '&dates=' . date('Y-m-d',strtotime('-2 Days')) . ',' . date('Y-m-d'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
$output = curl_exec($ch);
curl_close($ch);
$data = (object) simplexml_load_string($output,'SimpleXMLElement',LIBXML_NOCDATA);
$now = $data->feed->entry[count($data->feed->entry)-1]->attributes()->circulation;
$yes = $data->feed->entry[count($data->feed->entry)-2]->attributes()->circulation;
echo 'Subscribers: ' . $now . ' (' . (($now-$yes>0) ? '+' : NULL) . ($now-$yes) . ')';
?>
This will output:
Subscribers: 331804 (+471)
Another XBOX Snippet
This time this shows when you where “last seen” and what you were doing/playing. The snippet itself grabs the data from a script created by duncanmackenzie.net. The only downside to this snippet is that the data isn’t updated that often and on rare occasions doesn’t return any data, but I believe this is because of too many requests within a set time period. Remember to insert your gamertag instead of rentedsmile.
<?php
$ch = curl_init('http://xboxapi.duncanmackenzie.net/gamertag.ashx?GamerTag=rentedsmile');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,4);
$data = curl_exec($ch);
curl_close($ch);
$xml = new SimpleXmlElement($data,LIBXML_NOCDATA);
foreach ($xml->PresenceInfo as $mystatus) {
echo '<div id="xboxlivestatus">' . $mystatus->StatusText . ' : ' . $mystatus->Info . ' </div>';
}
?>
This will output:
Offline : Last seen 12/07/09 playing Left 4 Dead 2