preg_match Follower Count on Google+ - php

I searched online for a while and there is no way right now to get the Follower count of Google+ Profiles.
My plan now was to get the webpage code with file_get_contents and then to use preg_match.
But I´m using this function the first time and I have no clue how to use it. I read online a bit about it but I dont understand it.
The pattern is quite simple. There is a Number with periods/dots (.) on every thousand, a space and then the word "Follower".
How can I express that as the pattern for preg_match ?
And I read something about preg_replace which I could then use to replace the periods/dots (.) with nothing. Am I right ?
Thanks a lot !
Regards
Selfster

There is an API for that.
Here is the reference.

It's not a fool proof method but it works.
$test = file_get_contents("https://plus.google.com/+SundarPichai");
$res = preg_match_all("%\b([\.\,\d]+) followers%",$test, $output, PREG_SET_ORDER);
var_dump($output);
The regex "%\b([\.\,\d]+) followers%" starts with a non alphanumeric character and than takes commas and points (depending on the localization).
I think it by default falls back to the American (thats why there are comma's) and than followed by a space and the word followers

First, you need a Google API key, then it works like this:
id is like 103382528845345115881
key like AIzaSyAqlZ1MJSGXMSs8a5WbfvLpZTGJeHLVc2w
https://www.googleapis.com/plus/v1/people/<id>?key=<key>
The field you need is this: "circledByCount": 10,
Shortes form to use is like this:
<?php
$followers = json_decode(file_get_contents($url), true);
$followers = isset($followers['circledByCount'])
? $followers['circledByCount'] : 0;
Only works when the followers amount is publicily visible.

Related

PHP - Searching strings from a certain format

I've seen other examples using regex however I'm a little bit troubled trying to format mine correctly, I'll have a list of numbers like this:
1.0.0.1ACS
1.0.0.2ADS
1.0.1.8AAB
However I only want to have the actual 4 numbers but the numbers could turn into multiple digits per line for example 122.222.222.222 (up to 3) how would I go about using PHP to do this? I presume I would have to put them all into a array first then for each in array, then I am confused on how to remove the extra letters.
Thanks in advance!
Yours Code is:
<?php
$input = <<<TEST
1.0.0.1ACS
1.0.0.2ADS
1.0.1.8AAB
TEST;
if (!preg_match_all("#(\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})+#", $input, $matches))
print("NOT FOUND!");
else
var_dump($matches[0]);

PHP preg_match for grabbing YouTube ID from img.youtube.com too

I have implemented a function for grabbing Youtube ID and i use the regular expression:
preg_match('%(?:youtube(?:-nocookie)?\.com/(?:(?:v|e(?:mbed)?)/|.*[?&]v=|[^/]+/.+/)|youtu\.‌​be/)([^"&?/ ]{11})%i', $sourceCode, $youtube))
My problem is that this resular expression doesn't grab the id from a URL like: http://img.youtube.com/vi/Di9mW35zprs/0.jpg
So, how can i change it to work as it works right now but by giving me the "Di9mQ35zprs" from a URL "http://img.youtube.com/vi/Di9mW35zprs/0.jpg" too?
THERE ARE MANY OTHER QUESTIONS REGARDING GRABBING YOUTUBE ID WITH PREG_MATCH, BUT NOONE OF THEM ANSWERS HOW TO GRAB FROM A URL http://img.youtube.com/vi/Di9mW35zprs/0.jpg TOO.
All i want to achieve is to replace my current preg_match and enable it to grab youtube ID from http://img.youtube.com/vi/Di9mW35zprs/0.jpg urls.
Thank you!
Use parse_url() and explode() first. Now your regexp looks like it came straight from hell.
try using this:
$videoId = preg_replace("/.*v[=i]\/?(\w+).*/", '\1', "http://img.youtube.com/vi/Di9mQ35zprs/0.jpg");
The function will return the id you want. It should work with both types of URL you described.

getting number from external website

I need to get a number from this website: Current STC price which displays a market driven figure: STCs.
i tried this:
$html = file_get_contents('http://www.greenenergytrading.com.au/certificates/todays-pricing');
$html = strip_tags($html);
which leaves me with a long string. I then tried to remove anything before the figure I'm after, assuming that the text wont change:
$html = preg_replace('/.*Current STC price/', '', $html);
However, this doesnt work. it seems to work on online RexExp tester but not in production. also, is this a reasonable approach?
cheers
You can use preg_match with the $matches parameter provided to extract all ocurrences of from the website source and store them in an array. Then, just access the first element of the array.
Check out the documentation for preg_match here:
http://php.net/manual/en/function.preg-match.php
EDIT: Oh, I just saw your comment that you tried preg_match already. What regexp's have you tried? Have you tried something like "/$[0-9]{1}/" ?

PHP preg_replace_callback correct escaping to handle hash symbol ('#')

I'm doing some work with a Twitter feed and want to turn any hashtags into a clicable URL.
A hashtag is a hash symbol ('#') immediately followed by a word acting as a search tag - and contains no spaces.
An example would be ...
#Eutechnyx looking to form a tech group in #Shoreditch next year. Game and Web programmers get in touch. #AutoClubRev
There are two tags here, #Shoreditch and #AutoClubRev.
These should respectively become the following links ...
https://twitter.com/#!/search?q=%23Shoreditch
and
https://twitter.com/#!/search?q=%23AutoClubRev
I'm assuming I should be using preg_replace_callback here and not just vanilla preg_replace, as I am trying to take a backreference ($1) and change it not just display it. But of course I could be wrong. I'm not fuessed on which function to use - as long as it does the job and is relatively efficient.
Thanks,
Pete
preg_replace should be able to do it.
$test = "#Eutechnyx looking to form a tech group in #Shoreditch next year. Game and Web programmers get in touch. #AutoClubRev";
echo preg_replace('|#([\w_\d]+)|', '#\1', $test);

removing phone number from a document

I've got a challenge that I am hoping that the SO community is able to help me with.
I trying to parse a lot of html documents in my PHP application to remove personal details, such as names, addresses and phone numbers. I can remove most of these details without too much trouble, however the phone number is a real problem for me.
My idea is to take the text from these documents and the use a regex to identify the phone numbers and replace them with another value such as 'xxxx'.
I've got 2 regex that I am using one for UK landline numbers and one for UK cell/mobile numbers.
However when I try and run them against the text it just returns an empty string.
I am using the following preg_replace code:
$pattens = array(
'/^(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$/',
'/^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$/'
);
$replace = array('xxxxx', 'xxxxx');
//do the search for the numbers.
$updatedContents = preg_replace($pattens, $replace, $htmlContents);
At the moment this is causing me a lot of head scratching as I thought that I had this nailed, but at the moment I can't see what's wrong??
I am sure that it is something really simple.
Thanks,
Grant
You probably don't want to anchor your regular expressions. Remove the ^ from the beginning and the $ from the end.

Categories