Sort an Object Array in PHP

This gives me a brain ache every time and I always forget what project I used it in last so here it is. Still don’t quite understand why php cant just have a built in more readable function.


//$adverts is our object array

usort($adverts, function($a, $b)
return ($a->price < $b->price);

Get YouTube Video Data with PHP

This is amazing simple. I always thought oh no another API and if its anything like Facebooks API then you have to learn something new every month! But this is amazingly easy!

You just need to pull the information you want from You Tube via ‘simplexml‘ in PHP

YouTube has loads of different RSS feeds, in this case I am just using the one that gets all data about a single video.

//your video id

// get xml
$video_feed = file_get_contents("$v");
$sxml = new SimpleXmlElement($video_feed);

//set up nodes
$namespaces = $sxml->getNameSpaces(true);
$media = $sxml->children($namespaces[‘media’]);
$yt = $media->children($namespaces[‘yt’]);
$yt_attrs = $yt->duration->attributes();

$video_title = $sxml->title;
$video_description = nl2br($sxml->content);
$video_keywords = $media->group->keywords;
$video_length = $yt_attrs[‘seconds’];

Return string between two characters in the given string PHP

Sometimes I find it useful to return only a certain part of a string identified by two characters in that existing string.

Here is the useful function to achieve this:

function substring_between($haystack,$start,$end) {
if (strpos($haystack,$start) === false || strpos($haystack,$end) === false) {
return false;
} else {
$start_position = strpos($haystack,$start)+strlen($start);
$end_position = strpos($haystack,$end);
return substr($haystack,$start_position,$end_position-$start_position);