Extraction d’une sous-chaine :
string substr ( string $string
, int $start
[, int $length
] )
exemple : $ligne=substr($str,strpos($str,’;’)+1);
Recherche d’une chaine entre deux autres :
function getStringBetween($str,$from,$to)
{
$sub = substr($str, strpos($str,$from)+strlen($from),strlen($str));
return substr($sub,0,strpos($sub,$to));
}
Affichage d’une log dans la console JAVASCRIPT :
<?php
/**
* Send debug code to the Javascript console
*/
function debug_to_console($data) {
if(is_array($data) || is_object($data))
{
echo("<script>console.log('PHP: ".json_encode($data)."');</script>");
} else {
echo("<script>console.log('PHP: ".$data."');</script>");
}
}
?>
Récupération de texte dans une page HTML :
<?php
$parligne = $_GET["parligne"];
$max = $_GET["max"];
$ligne=$parligne;
//--
$my_file = 'terminus.txt';
//-- 'a' = append , 'w' = write and create !
$handle = fopen($my_file, 'a') or die('Cannot open file: '.$my_file);
//--
function debug_to_console($data) {
if(is_array($data) || is_object($data))
{
echo("<script>console.log('PHP: ".json_encode($data)."');</script>");
} else {
echo("<script>console.log('PHP: ".$data."');</script>");
}
}
//--
function test_print($item2, $key)
{global $handle; //-- Récupération variable globale
if ($item2!="DEBUT" && $item2!="FIN" && (strpos($item2,'div') == false) && (strpos($item2,'passages') == false) )
{$result=$item2;
$txt="$result\r\n";
echo "$result<br />\n"; //-- affichage à l'écran du résultat
fwrite($handle, $txt); //-- écriture dans le fichier
}
}
//--
for ($numligne=$parligne;$numligne<$max;$numligne++)
{$ligne=$numligne."";
//debug_to_console("Ligne:".$ligne);
$html=file_get_contents('http://wap.ratp.fr/siv/schedule?service=next&reseau=metro&linecode='.$numligne);
//-- Bus : http://wap.ratp.fr/siv/schedule?referer=line&service=next&reseau=bus&stationname=&linecode=
//-- Metro : http://wap.ratp.fr/siv/schedule?service=next&reseau=metro&linecode=
if(preg_match('%.*?<html[^>]*>.*?<head>.*?<title>.*?</title>.*?</head>.*?<body[^>]*>(.*?)</body>.*?</html>%si',$html,$m))
$body=$m[1];
else
$body='';
$strfin0=str_replace ('<b class="bwhite">','|DEBUT|'.$ligne.';', $html);
$strfin1=str_replace ('Ligne non reconnue','|FIN|',$strfin0);
$strfin2=str_replace ('</b></div><div class="space3">','|FIN|',$strfin1);
$pieces = explode("|", $strfin2);
array_walk($pieces, 'test_print');
}
//--
fclose($handle);
?>
Votre commentaire