Forum Moderators: phranque
I need to create some kind of script that will go to a YouTube Channel or Video page, grab the number of Total Views, (or Comments or Channel Subscribers), bring it back to my server, and drop the data into a file that can be read by other scripts.
I have been trying to come up with a way to do this, but so far a solution has eluded me.
Does anyone an idea of how to make this work? Or, if it's impossible, could you tell me so I stop burning my brain trying to think of a way? :)
Thanks!
Spanger
Seriously though, they're an extremely powerful tool. For instance, to find all integer numbers in a document, you could do...
$strToSearch = "123asdf 456 foobar 987";
preg_match( "/([0-9]+)/", $strToSearch, $matches );
This would fill $matches with an array consisting of all integers in $strToSearch.
$matches[1] == "123"
$matches[2] == "456"
$matches[3] == "789"
The "/([0-9]+)/" string is the regular expression. Basically, it will search for an instance of a number between 0 and 9, then capture that number and all numbers after it.
Another example... "/([0-9\.]+)/" would capture decimal numbers as well as integers.
A third example... "/([^0-9])/" would capture all characters that are NOT a number.
When I load the script curl.php in my browser, I get this error:
---
Warning: curl_exec() has been disabled for security reasons in /home/spanger/public_html/curl.php on line 8
---
This is what my script looks like:
--------
<?php$location = curl_init("http://www.google.com/");
$file = fopen("google.php", "w");
curl_setopt($location, CURLOPT_FILE, $file);
curl_exec($location);
curl_close($location);
?>
Any ideas?
Thanks!
Spanger
I cannot figure out how to define this.
Here's the part of the html that has the info I want:
<span class="smallText">Channel Views:</span> <b>3,224,726</b><br/> The thing is, the number is always changing. I need to define the beginning and end and grab the stuff in between.
My trouble is that I can't figure out HOW to define the beginning and end. How do I do this?
Here's the code I'm working on.
<?php
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, "http://youtube.com/profile?user=UsErNaM3");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec ($curl);
curl_close ($curl);
$strToSearch = $result;
preg_match( "problem spot", $strToSearch, $matches );
?>
Thanks,
Spanger
The "problem spot" should become...
'/Channel Views:[^0-9]*([0-9,]+)/i'
That will match the phrase "Channel Views:" followed by any string of non-numeric characters until it reaches a number, at which point it will capture as many numeric characters and commas as possible until it hits a character that is not a comma or number.
Do a print_r or var_dump on $matches to see what it gives you.
[edited by: WesleyC at 3:20 am (utc) on Aug. 14, 2007]
when I do var_dump I get this:
array(2) { [0]=> string(34) "Channel Views: 3,230,588" [1]=> string(9) "3,230,588" } print_r gives this:
Array ( [0] => Channel Views: 3,230,617 [1] => 3,230,617 ) I may be REALLY dense, but... How do narrow these down to just the numbers and commas?
I am trying to get it down to just the numbers, so I can print it to a file and use that file as an include on any number of pages.
Thanks for the help so far! =D
Spanger
To put the results into a file, just use something like...
file_put_contents( "myViews.txt", $matches[1] );
Enjoy!
[1][edited by: WesleyC at 7:01 pm (utc) on Aug. 15, 2007]
Got another regular expressions question.
I need to grab the number out of this line:
html:
<span>Videos (<a href="/profile_videos?user=RonPaul2008dotcom" class="headersSmall">42</a>)</span> Videos (42) I would assume I would use this:
preg_match( '/Videos ([^0-9]*([0-9,]+)/i', $strToSearch, $vidmatches ); But that returns this error when I try to run it:
Warning: preg_match(): Compilation failed: missing ) at offset 24 in /home/ytinfo/public_html/ronpaul.php on line 17
It thinks the "(" is part of the command, not part of the string.
How can I get around this?
Thanks!
Spanger
Running it like this:
$strToSearch = $result;
preg_match( '/Videos \([^0-9]*([0-9,]+)/i', $strToSearch, $vidmatches );
var_dump ($vidmatches); Gives me this when I do var_dump:
9array(2) {
[0]=>
string(49) "Videos (<a href="/profile_videos?user=RonPaul2008"
[1]=>
string(4) "2008"
}
It works dandy, but it grabs the number out of their username. =S
How can I get around this one? Is there a way to specify "but not this number"?
Source I'm coming from again:
<span>Videos (<a href="/profile_videos?user=RonPaul2008dotcom" class="headersSmall">42</a>)</span> Thanks for all your help! =D
Spanger
[edited by: Spanger at 9:46 pm (utc) on Aug. 17, 2007]