Hello! I'm attempting to create a application that randomly generates strings and tags it on the end of http://youtube.com/watch?v=.... but the links it generates links to a non existant video. I noticed there are more capital letters in their URL's but that's the only difference I can see. Here is my PHP.

$charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVQXYZ1234567890';
$url = substr(str_shuffle($charset), 0, 11);
$url = substr(str_shuffle($url.'_'), 0, 11);
echo $url;

It's a Ajax request so thats why it's being echoed.
That is what it does atm.
Anyways any ideas? Thanks for any help!

4 Years
Discussion Span
Last Post by veedeoo


I noticed there are more capital letters in their URL's but that's the only difference I can see.

I'll be honest with you. I got no idea what you are trying to do. But if you want to get random letters & numbers.

Then you should used str_replace() function instead of substr() function

Here is a link about str_replace() function


There are some examples that might help you.


Look this is what I'm trying to do. I'm trying to create a string to tag on the end of a youtube video url so http://youtube.com/watch?v= [generated string here]. I have that working, but it always links to non existant videos.
That ^^ is an example of one my system generated but is a invalid URL. So what I'm trying to do is get it to generate valid youtube video URL's if that helps any.



So what I'm trying to do is get it to generate valid youtube video URL's if that helps any.

That will be impossible, the only way to make it valid is the videos are upload on your website and you provide a random uniqie id to each video. If you need to get Youtube video url then you need to get the embed youtube url. There's no way that you can duplicate a Youtube video url base on your code.


I aplogize for the interruption. Is this what you're trying to achieved? Click Here. That's random video from youtube with at least 99.00% accuracy.. of course, the remaining 01.00% can be given to the script load error.

Edited by veedeoo: more info. added


Yes veedeoo that's almost exactly what I am trying to achieve! Any ideas, or was that just a point?



We cannot directly re-create or generate a youtube id for this matter, because youtube is using a simple yet very effective method of generating video ID. It is so simple that you already done it on your script. However, the hardest one to match is the output of something that has no alogarithm at all. It is pretty much divided into to three random arrays to create dashes, caps, lower case, and numbers. So, the randomness can be something like this generate dash,generate random 2 upper case letters, generate 2 or single integers followed by random lowercase, generate the mix of the three in random. Then this ID is then match to the database if ever exist and if it does not, that becomes a unique ID. Unlike other video sharing website e.g. p**nhub ( it is not that I like visiting this type of site, but I was envolved in developing an id snipper script for this), which is pretty predictable.

How do I know all about these? Because my older brother Michael went to school and used to work with one of the founder of Youtube way back in the early years of paypal. Inspite of the change of ownership which is now google, I still believe that the video ID generator function has not changed at all or maybe at the very minimum to this date.

The easy method I used in my demo link is to use my minified version of the youtube API class I wrote sometime ago ( i mean years ago).

Just for the pupose of responding to this thread I modified it so that it can work like a random.

Let's take a look at the minified version of the class I wrote before.. this is intended to be non-Zend module dependent. This class pick up the latest video from youtube.

    ## filename: minified YouTubeVideo.class.php
    ## API Type: Zend-Less by veedeoo 2012. (Does NOT need Zend module to run, just plain PHP :))
    ## Author's Common Web Portals hang-out , just in case you have questions: tutpages.com, veedeoo.com, phpclasses.org and daniweb.com
    ## License: Use it as deemed appropriate as suggested in TOS of Youtube API. 
    ## Restriction 1: As per Google and Youtube policy, any video delivered by this script ( full version or minified version of veedeoo's YouTubeVideo.class.php) "MUST NOT BE LOADED IN ANY PLAYER OTHER THAN YOUTUBE PLAYER".
    ## Restriction 2: Videos embedded by this script as swobject or iframe "MUST NOT CONTAIN ANY OTHER ADVERTISEMENT, OTHER THAN PLAYER ADS COMING FROM YOUTUBE".
    ## Warranty: This script has no warranty of any kind.
    ## Credits: Veedeoo from tutpages.com and veedeoo.com.
    ## Revision Date: November 1, 2012

    class YouTubeVideo{
    ## let's do some singleton
    ## Can add namspace here if you want.. why not? :)
    public static $VedeooInstance = NULL;
    ## items below has been overridden.
    private $url = null;
    private $q = null;
    private $index = null;
    private $orderby = null;
    private $i = null;
    private $page = null;

    public static function getVedeooInstance() {
            if(!isset(self::$VedeooInstance)) {
              self::$VedeooInstance = new YouTubeVideo();
            return self::$VedeooInstance;


    ## using singleton, we need to set the constructor private
    private function __construct(){


    private function randomized(){
    ## You must replace the rockStars array to your own chosen tags or category.
            $rockStars = array(
        'The Beatles','Elvis Presley','James Brown','Rolling Stones','Bob Dylan','Chuck Berry','The Who','Led Zeppelin','Stevie Wonder','Jimi Hendrix','Ray Charles','The Beach Boys','Pink Floyd','Aretha Franklin','Little Richard','Marvin Gaye','Bruce Springsteen','David Bowie','Fats Domino','Black Sabbath','Queen','Buddy Holly','Bob Marley','Sam Cooke','Elton John','Neil Young','U2','Run-D.M.C.','Bo Diddley','Jerry Lee Lewis','B.B. King','Sly & The Family Stone','The Clash','Prince','The Grateful Dead','The Velvet Underground','Nirvana','Michael Jackson','The Supremes','The Temptations','Madonna','Public Enemy','The Kinks','Otis Redding','The Everly Brothers','Van Halen','Elvis Costello','Simon & Garfunkel','Cream','Frank Zappa','Roy Orbison','Eric Clapton','George Clinton & Parliament/Funkadelic','The Allman Brothers Band','Janis Joplin','The Ramones','Crosby, Stills, & Nash & Young','Fleetwood Mac','AC/DC','The Byrds','Joni Mitchell','The Eagles','Smokey Robinson & The Miracles','R.E.M.','Creedence Clearwater Revival','Johnny Cash','Van Morrison','Aerosmith','Curtis Mayfield & The Impressions','The Drifters','The Police','Metallica','Pearl Jam','Deep Purple','Bill Haley & His Comets','The Band','Santana','Yes','Jefferson Airplane','King Crimson','Al Green','The Isley Brothers','The Moody Blues','The Sex Pistols','N.W.A','Talking Heads','Tom Petty & The Heartbreakers','Billy Joel','The Bee Gees','The Yardbirds','The Four Tops','Radiohead','Patti Smith','Guns N Roses','Chicago','Rush','Bob Seger & The Silver Bullet Band','Rick Nelson','Earth Wind & Fire'
            return $rockStars[0];
    public function getCount(){
      ## get summary counts from open search: namespace
      ## Use this to determine if the query from randomizer returns any result.
      $this->counts = $this->sxml->children('http://a9.com/-/spec/opensearchrss/1.0/');
      return $this->counts->totalResults; 


    public function getVideo(){
            $this->q = $this->randomized();
            ## Variables override to bypass _constructor.. we don't need it for getting a single video
            $this->index = 1;
            $this->orderby = "published";
            $this->i = 1;
            $this->url =  "http://gdata.youtube.com/feeds/api/videos?q={$this->q}&start-index={$this->index}&orderby={$this->orderby}&max-results={$this->i}";
            $this->sxml = simplexml_load_file($this->url);
            $item = array();
        foreach ($this->sxml->entry as $entry) {
            ## get nodes from media: namespace to get media info.
            $this->media = $entry->children('http://search.yahoo.com/mrss/');

            ## get url of the youtube video player 
            $attrs = $this->media->group->player->attributes();
            $item['vurl'] = $attrs['url']; 
            $this->v_url = $attrs['url'].'&';
            $this->pattern = '/v=(.+?)&+/';
            preg_match($this->pattern, $this->v_url, $this->matches);
            $item['v_id'] = $this->matches[1];
            ## get video thumbnail if available
            $this->attrs = $this->media->group->thumbnail[0]->attributes();
            $item['thumb'] = $this->attrs['url']; 
            $this->v_items[] = $item;

            return $this->v_items;



You can change $rockStars = array(), to your own tags or youtube category, for the purpose of providing the random tags or video.

To use the class just include the file above and then, we can instantiate the class as simple as this


        $video = YouTubeVideo::getVedeooInstance()->getVideo();

        foreach($video as $vidz){
        ## get the video id
        echo $vidz['v_id']; 


If you want to load in the form input, then the url for youtube will be like this..

        <input type="button" onClick="parent.location='http://www.youtube.com/watch?v=<?=$vidz['v_id']?>'" value="go">

That's pretty much it.... please respect the goole/youtube API TOS... they are pretty serious in implementing it.

good luck to you..


It is working, I just came from that page to this page. It might be the server issues with my hosting company, but I cannot complain very often, because I am only paying them $11.00 dollars per year. So, I practically getting what I have paid for ( I guess)..

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.