All pastes #550750 Raw Edit

Something

public text v1 · immutable
·published 2007-06-08 15:00 UTC
rendered paste body
<?php
/**
* Google search parser est un google sans passer par leur api
* Réalisation : Nans Stefanini -> http://nans.stefanini.free.fr
* Blog : http://www.22h22.fr
*/


//renvoie une url longue raccourcie
function ShortDisplayURLggp($FullPath,$MaxLenght=65)
{
	If (strlen($FullPath) > $MaxLenght)
	{
		//Suppression des arguments et fragment si néces"saire
		if (strpos($FullPath,";#",0) > 0) $FullPath = substr($FullPath,0,strpos($FullPath,"#",0));
		if (strpos($FullPath,";?",0) > 0) $FullPath = substr($FullPath,0,strpos($FullPath,"?",0));

		If (strlen($FullPath) > $MaxLenght)
		{
			// Decompose RL
			$FullPathParsed=parse_url($FullPath);
			// Parties minimales
			$RootPath = $FullPathParsed['scheme'].'://'.$FullPathParsed['host'];
			// Slash de fin si dossier
			if (substr($FullPathParsed['path'],-1) == '/')
			{
				$FullPathParsed['path'] = substr($FullPathParsed['path'],0,-1);
				$isFolder = TRUE;
			}
			else
			{
				$isFolder = FALSE;
			}
			$PathBaseName = basename($FullPathParsed['path']);
			$FullPathParsed['path'] = substr($FullPathParsed['path'],0,-1*strlen($PathBaseName)-1);
			// Pas besoin de citer ndex
			if (($PathBaseName == 'index.html') OR ($PathBaseName == 'index.php') OR ($PathBaseName == 'index.php3'))
			{
				$FullPathParsed['path'] = substr($FullPathParsed['path'],0,-1*strlen($PathBaseName)-1);
				$PathBaseName = basename($FullPathParsed['path']);
				$isFolder = TRUE;
			}

			if ($PathBaseName == '')
			{
				if (strlen($RootPath) > $MaxLenght) $RootPath = $FullPathParsed['scheme'].'://'.ShortDisplayHostggp($FullPathParsed['host'],$MaxLenght - strlen($FullPathParsed['scheme']) - 3);

				return $RootPath;
			}
			else if ($FullPathParsed['path'] == '')
			{
				return $RootPath.'/&#8230;/'.$PathBaseName;
			}
			else if ((strlen($RootPath)+strlen($PathBaseName)+3) >= $MaxLenght)
			{
				return $RootPath.'/&#8230;/'.$PathBaseName;
			}
			else
			{
				// Reduction du chemin
				while ((strlen($RootPath)+strlen($PathBaseName)+strlen($FullPathParsed['path'])+3) > $MaxLenght)
				{
					$FullPathParsed['path'] = substr($FullPathParsed['path'],0,-1*strlen(basename($FullPathParsed['path']))-1);
				}

				if ($FullPathParsed['path'] == '')
				{
					return $RootPath.'/&#8230;/'.$PathBaseName;
				}
				else
				{
					return $RootPath.$FullPathParsed['path'].'/&#8230;/'.$PathBaseName;
				}
			}
		}
		Else
		{
			return $FullPath;
		}

	}
	Else
	{
		return $FullPath;
	}
}

function ShortDisplayHostggp($FullHost,$MaxLenght=80)
{
	If (strlen($FullHost) > $MaxLenght)
	{
		if (substr_count($FullHost,'.') >= 2)
		{
			return $FullHost;
		}
		else if (substr_count($FullHost,'.') == 1)
		{
			$extension = substr($FullHost,strpos($FullHost,".",0));
			$FullHost = substr($FullHost,0,strpos($FullHost,".",0));
			$FullHost = substr($FullHost,0,10).'(&#8230;)'.substr($FullHost,-1*(10+3+1+strlen($extension))).'.'.$extension;
		}
		else
		{
			$FullHost = substr($FullHost,0,$MaxLenght-3).'(&#8230;)';
		}
	}
	else
	{
		return $FullHost;
	}
}

function ShortDisplayNameggp($FullName,$MaxLenght=50)
{
	If (strlen($FullName) > $MaxLenght)
	{
		$FullName = substr($FullName,0,$MaxLenght-3).'(&#8230;)';
		return $FullName;
	}
	else
	{
		return $FullName;
	}
}

?>
<?php




function googlesearch($req, $start='0',$lg='fr') {

$index = @file_get_contents('http://www.google.fr/search?q='.urlencode($req).'&hl='.$lg.'&start='.$start);
//echo tp://www.google.fr/urlencode($req
$index = str_replace ('<b>', '', $index);
$index = str_replace ('</b>', '', $index);

/*
Résultats <b>1</b> - <b>10</b> sur un total d'environ <b>1 780</b> pour <b>cogestar</b>  (<b>0,15</b> second
Résultats <b>61</b> - <b>66</b> sur <b>66</b> pour <b>cogestar</b>.  (<b>0,79</b> secondes) */
  // nombre de liens trouvés
   preg_match_all('%sur[\s]*(un total d\'environ)* (([0-9 ]*)*) pour%si', $index, $nbliens);
 
 /*

*/
$nblien = $nbliens[2][0];
$nblien = str_replace (' ', '', $nblien);


// si la requette ne retourne rien on quite la fonction
if (!isset($nblien)) {
  return 'false';
  break;
}

 preg_match_all('%\<div class=g\>(.*?)(\</div\>)%si', $index, $out);
            
 
 /*
 echo '<pre>';
 print_r($out);
 echo '</pre>';
*/

$tabfin = array();
foreach ($out[1] as $key => $value) {
  
  /*
     preg_match_all('%(\<h2 class=r.*\>)?(.*)?\</h2\>%si', $value, $out2);
   
    echo '<pre>';
    print_r($out2);
    echo '</pre>';
*/

  // description
  $q = '\<td class="j"\>.*(\>)(..*)\<br';
   // preg_match_all('%%si', $value, $out3);
  
  
  // liens et titre
  $p = '\<h2 class=r\>\<a href="(.*?)".*\>(..*)\</h2\>';
  
  // lien titre + description
  preg_match_all('%'.$p.'.*'.$q.'%si', $value, $out2);
  
  /*
  echo '<pre>';
    print_r($out2[1]);
    print_r($out2[2]);
    print_r($out2[4]);
    echo '</pre>';
  echo '<br /><br /><br />';
  */
  
  $tabfin[$key]['lien'] = $out2[1][0];
  $tabfin[$key]['titre'] = $out2[2][0];
  $tabfin[$key]['description'] = $out2[4][0];
  $tabfin[$key]['nbliens'] = $nblien;
  
  /*
  // pour rechercher les liens verts
   preg_match_all('%\<span class=a\s*\>\s*([-A-Z0-9+&@#/=~_|!:,.;\%]*)?.*(\</span\>)%si', $value, $out3);
 echo '<pre>';
    print_r($out3);
    echo '</pre>';
    */
    
 
}
/*

      */ 
   
   /*
    echo '<pre>';
    print_r($tabfin);

    echo '</pre>';
  echo '<br /><br /><br />';
  */
  
  

   
   
return $tabfin ;
 
  
}




/* exemple de fonctionnement */


?>
<form action="" method="get" >
<input type="text" size="40" name="req" id="req" value="<?php if (isset ($_GET['req']) ){ echo $_GET['req'] ; }?>">
<input type="submit" name="submit" value="Rechercher google"  >
</form>
<script>
  document.getElementById("req").focus();
</script>
<?php
if (!isset ($_GET['pagestartgg']) ){
$_GET['pagestartgg'] = 0;
}

if (isset($_GET['req'])) {

$tabgg =  googlesearch($_GET['req'],$_GET['pagestartgg']);

echo 'Résultats de votre recherche : ';
echo '<a href="http://www.google.fr/search?q='.urlencode($_GET['req']).'&start='.$_GET['pagestartgg'].'" target="_blank">Voir sur google</a><br /><br />';

foreach ($tabgg as $key => $value) {
  echo '<strong><a href="'.$value['lien'].'">'.$value['titre'].'</a></strong><br />'.$value['description'].'<br /><a href="'.$value['lien'].'">'.ShortDisplayURLggp($value['lien']).'</a><br /><br />';
}

$nbliens = $tabgg[0]['nbliens'];
echo '<br />';

// mise en place des pages
$nbpage = round($nbliens/10);
echo '<strong>'.$nbpage. ' pages </strong><br />';

$ideb = ($_GET['pagestartgg']/10)-10 ;
$ideb < 0 ? $ideb = 0: '';
$page = ($_GET['pagestartgg']/10);
//echo 'page '.($_GET['pagestartgg']/10).'<br />';


$nbpage < $ideb ? $ideb = $nbpage : '';
//echo 'ideb '.$ideb.'<br />';

$ifin = $ideb + 15 ;
$ifin > $nbpage ? $ifin = $nbpage : '';

echo '<a href="?pagestartgg=0"><<</a>   ';
  for ($i = $ideb; $i<=$ifin; $i++) {
    if ($page == $i)  echo '<strong>  ' ;
    echo '<a href="?pagestartgg='.($i*10).'&req='.urlencode($_GET['req']).'">'.($i+1).'</a> ';
      if ($page == $i)  echo '  </strong>' ;
  }
echo '   <a href="?pagestartgg='.$nbpage.'">>></a> ';

}
?>