Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Script that shows number of YouTube subscribers?

Featured Replies

Hello, making a small website listing some of the social networking websites I am on. And I would like to show the number of YouTube subscribers I have on the page, how can I extract the code that youtube uses on my channel to show my subscribers, or are there any other scripts that do this?

 

And also I would like to display how many videos I have, help?

Have you read the youtube API documentation?

Youtube's API doesn't stretch as far as this unfortunately. The best thing you can do is to scrape the value from your page

 

$url = 'http://www.youtube.com/cbgfilms';
$content = file_get_contents($url);
preg_match('%Subscribers:</span> \K\d+%', $content, $matches);
echo $matches[0];

 

That should do it

 

I would recommend possibly updating the value via cron every hour to a stored location of some sort rather than run this code every visit

$url = 'http://www.youtube.com/cbgfilms';$content = file_get_contents($url);preg_match('%Subscribers:</span> \K\d+%', $content, $matches);echo $matches[0];

 

Just out of curiosity what does the "/K/d+%" do ?

% is the delimiter for the regex (so its on either side of it)

 

Subscribers:</span> searches for that literal text

\K removes all text before it from the matched output

\d+ matches digits after the rest of the match until a non digit is found

 

So basically the regex matches that whole text with the digit, but clears everything before the digits

Awesome thanks, are those types of things called delimiters ?

 

I need to read up on them. They look like they are really useful :)

  • Author

Youtube's API doesn't stretch as far as this unfortunately. The best thing you can do is to scrape the value from your page

 

$url = 'http://www.youtube.com/cbgfilms';
$content = file_get_contents($url);
preg_match('%Subscribers:</span> \K\d+%', $content, $matches);
echo $matches[0];

 

That should do it

 

I would recommend possibly updating the value via cron every hour to a stored location of some sort rather than run this code every visit

 

Hello Jay, thanks for the code works almost, but it shows the subscriber number of my 'other channel' which is listed on the page, any way to make it show my subscribers?

Can you post the link of the page and the value it has at the moment (or a picture of where it is on the page)

 

@JamesW - The delimiters are what the regex is enclosed in. More often than not you will see people using / such as

/regex-here/

but it's just the same as

%regex-here%

I prefer using the % since if you need to match your delimiter in your regex you have to escape it, and as I generally work with url's and HTML which have /'s in them its not ideal

 

Example:

With / delimiters to match a closing </p> tag

/<\/p>/

With % delimiter

%</p>%

I realise from that example that it doesn't look like much, but when you're doing that for a lot of tags it can get tedious, and you use % a whole lot less

 

For some regex examples, take a look at the videos over at http://www.phpvideotutorials.com/free/ (lesson 11)

  • Author

@cbg - change your regex to

%subscriber_count">\K[\d,]+%

 

works perfectly now, thanks!

Also I'd like to show the number of videos I have, and the number of total upload views, is that possible too?

Oh also I wanna display the number of twitter followers and facebook fan page fans I have.

Sure it is. Just regex those out as well (if you're not sure about regex's take a look at that link I posted above)

  • Author

Sure it is. Just regex those out as well (if you're not sure about regex's take a look at that link I posted above)

 

Checked out the link and kinda got a little bit of it...

 

I have got the 'total video views' working fine now, but having trouble with the followers part.

 

So far I have:

preg_match('%follower-count%', $content, $matches);

ok so now you need to keep matching until just before the value you want, then use \K to cut off the currently matched text and then match the value you want. \d will match digits, and just a comma will match a comma, so put those in a character class [ ] and put a + after it to match until no number/comma is found. So after the \K put

[\d,]+

 

It's very basic stuff

  • Author

ok so now you need to keep matching until just before the value you want, then use \K to cut off the currently matched text and then match the value you want. \d will match digits, and just a comma will match a comma, so put those in a character class [ ] and put a + after it to match until no number/comma is found. So after the \K put

[\d,]+

 

It's very basic stuff

 

Agh, this still isn't working for me, I must be doing something wrong, hmm.

<?php $url1 = 'http://www.twitter.com/cbgfilms';
	  		$content1 = file_get_contents($url1);
	  		preg_match('%follower-count">/K[\d]+%', $content1, $matches1);
	  		echo $matches1[0];
         		?> followers<br />

 

I removed the comma as I don't have a follower count 1000 or more anyway, so there are no commas.

  • Author

any help here?

Yeah for getting twitter xml, use this

 

// creates an object to work with
$dom = new DOMDocument();
// Loads the xml data into the object
$dom->load('http://www.twitter.com/users/show/cbgfilms.xml');

// get all tags with "followers_count" as the tag
$count = $dom->getElementsByTagName('followers_count');
// Echo out the tag's value
echo 'Follower Count: '.$count->item(0)->nodeValue.'<br />';

// get all tags with "statuses_count" as the tag
$count = $dom->getElementsByTagName('statuses_count');
echo 'Status Count: '.$count->item(0)->nodeValue.'<br />';

 

  • Author

Cool I've got that now, now to trying to make a little XXX fans for the facebook portion.

 

As you mentioned before about a cron or something? Like caching the regex thing because it takes a while to load, even offline. Not sure how to do that.

Set up a cron job in your control panel (depends on what control panel and access you have to know how to do this). Generally just run

php /path/to/cron/script.php

to run the php script via the cron.

I would personally save all the data in an XML file or flat file and then just run that every time the page loads

  • Author

Set up a cron job in your control panel (depends on what control panel and access you have to know how to do this). Generally just run

php /path/to/cron/script.php

to run the php script via the cron.

I would personally save all the data in an XML file or flat file and then just run that every time the page loads

 

Turns out my hosting doesn't support cron jobs, any alternative way to do this?

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.