Skip to content

Instantly share code, notes, and snippets.

@merajsiddiqui
Last active May 4, 2018 06:21
Show Gist options
  • Save merajsiddiqui/17a4865775edd03d7df1851232c86afc to your computer and use it in GitHub Desktop.
Save merajsiddiqui/17a4865775edd03d7df1851232c86afc to your computer and use it in GitHub Desktop.
Ved parser to decode ved paramaters in google search result urls to find type of google result.
<?php
/**
* @author : Meraj Ahmad Siddiqui <[email protected]>
* @package: Ved Parser
* @description : This parser parse the ved parameter of the url form google to know its type
*/
function decodeVedParameter($vedParameter) {
$knownVedParams = [
'i' => 1,
't' => 2,
'r' => 6,
's' => 7
];
$decodeVedResult = [];
/**
* Handling Plain Ved
*/
if(substr($vedParameter,0,1) == 1) {
preg_match_all('/([a-z]+):([0-9]+)/i', $vedParameter, $matches, PREG_SET_ORDER);
foreach($matches as $match) {
$targetKey = isset($knownVedParams[$match[1]]) ?: $match[1];
$decodeVedResult[$targetKey] = (int) $match[2];
}
return $decodeVedResult;
}
/**
* handling base64 encoded Ved
*/
$pregPattern ='/([\x80-\xff]*[\0-\x7f])([\x80-\xff]*[\0-\x7f])/';
$decodedVed = base64_decode(str_replace(['_','-'], ['+','/'], substr($vedParameter,1)));
preg_match_all($pregPattern,$decodedVed,$matches, PREG_SET_ORDER);
foreach($matches as $match){
$targetKey = $targetValue = 0;
foreach(str_split($match[1]) as $index => $character){
$targetKey += (ord($character) & 0x7f) << $index * 7;
}
foreach(str_split($match[1]) as $index => $character){
$targetValue += (ord($character) & 0x7f) << $index * 7;
}
$decodeVedResult[$targetKey >> 3] = $targetValue;
}
return $decodeVedResult;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment