PDA

View Full Version : Parse URL


s0b
02-12-2007, 06:48 AM
Hi all, im using the anymedia plugin and under all videos I include the original URL to the video.


That i want is that url dont appear like this:

http://www .youtube.com/watch?v=AOEpkt7RZy0 (without spaces)

I want that it parse the name of the video (or the title of the web)

carlitos - el baile de la ducha (http://www .youtube.com/watch?v=AOEpkt7RZy0) (or YouTube - carlitos - el baile de la ducha (http://www .youtube.com/watch?v=AOEpkt7RZy0)).

Very much thanks

Ducks
02-12-2007, 10:03 AM
This will get the title from that page:

<?php
$url = 'http://www.youtube.com/watch?v=AOEpkt7RZy0';

$src = file_get_contents($url);
echo substr($src, strrpos($src, '<title>') + 7, strrpos($src, '</title>') - strrpos($src, '<title>'));
?>

s0b
02-12-2007, 10:34 AM
Great! very much thanks Duck!! that code i will insert it in the template or in the php?

Ducks
02-12-2007, 10:38 AM
Sorry I'm not able to give you that answer, but due performance issues you should grab the title when the link is posted and not everytime it's viewed as it would slow down your site a lot.

Analogpoint
02-12-2007, 04:30 PM
What you'd have to do is create a plugin that pulls the title from YouTube and saves it to the database, to then be displayed each time the page is loaded.

@Ducks:

echo substr($src, strrpos($src, '<title>') + 7, strrpos($src, '</title>') - strrpos($src, '<title>'));

You could you preg_match as an alternative to this. If you want to use the code you've written, stripos would be better because it's case-insensitive. BTW, why would you want to search starting at the end of the HTML document?

preg_match('%<title>([^<]+)<\/title>%i', file_get_contents ($url), $matches);
$title = $matches[1];