PHP

Pour écrire une page HTML dans une variable texte :

 header('Content-type:application/json;charset=utf-8');  
 $html=file_get_contents("https://www.elysee.fr/Agenda");  
 if(preg_match('%.*?<html[^>]*>.*?.*?.*?.*?<body[^>]*>(.*?).*?%si',$html,$m))  
      $body=$m[1];  
  else  
      $body='';  

Pour rechercher dans un tableau une chaîne de caractères et retourner la position dans le tableau :

 function customSearch($keyword, $arrayToSearch){  
   foreach($arrayToSearch as $key => $arrayItem){  
     if( stristr( $arrayItem, $keyword ) ){  
       return $key;  
     }  
   }  
 }  

Extraire un texte entre deux balises et retourner un tableau :

 function tag_contents($string, $tag_open, $tag_close){  
   foreach (explode($tag_open, $string) as $key => $value) {  
     if(strpos($value, $tag_close) !== FALSE){  
       $result[] = substr($value, 0, strpos($value, $tag_close));;  
     }  
   }  
   return $result;  
 }  
 $string = "i love cute animals, like [animal]cat[/animal],  
       [animal]dog[/animal] and [animal]panda[/animal]!!!";  
 echo "
";  
 print_r(tag_contents($string , "[animal]" , "[/animal]"));  
 echo "

« ;
//result
Array
(
[0] => cat
[1] => dog
[2] => panda
)

Lire un JSON en PHP :

 $jsonIterator = new RecursiveIteratorIterator(  
   new RecursiveArrayIterator(json_decode($json, TRUE)),  
   RecursiveIteratorIterator::SELF_FIRST);  
 foreach ($jsonIterator as $key => $val) {  
   if(is_array($val)) {  
     echo "$key:\n";  
   } else {  
     echo "$key => $val\n";  
   }  
 }  

test si une chaîne de caractères est présente dans une autre :

 $a = 'How are you?';  
 if (strpos($a, 'are') !== false) {  
   echo 'true';  
 }  

Nombre de jours entre deux dates :

<?php
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
?>

Donnera « +272 days » ;

Ou

 // ETAPE 1 : Initialisation des deux dates  
 // Date la plus ancienne au format AAAA-MM-JJ  
 $date_ancienne = new DateTime('2013-01-01');  
 // La date la plus récente, dans notre exemple la date du jour  
 $date_du_jour = new DateTime("now");  
 // ETAPE 2 : Calcul de la différence  
 $difference = $date_ancienne->diff($date_du_jour);  
 PUBLICITÉ  
 // ETAPE 3 : Affichage de la diff&eacute;rence  
 /* Le paramètre de la méthode interval->format() peut être '%y', '%m', '%d', '%h', '%i', '%s'; pour récupérer la différence en nombre d'années, mois, jours, heures, minutes ou en secondes */  
 // Dans notre exemple la différence sera exprimée en nombre de jours  
 $jours = $interval->format('%d');  
 echo 'La différence entre nos deux dates est de '.$jours.' jours';  

 

Liste des jours pour une année :

 // Appel : alldate.php?an=2019  
 //////////////////////////////////////  
 header('Content-type:application/json;charset=utf-8');  
 ///////  
 $an = $_GET["an"];  
 if (strlen($an)==0)     $an=2019;  
 /////  
 $tabmois = array("","janvier","février","mars","avril","mai","juin","juillet","août","septembre","octobre","novembre","décembre");   
 $tabjour = array("dimanche","lundi","mardi","mercredi","jeudi","vendredi","samedi");   
 ////////  
 $secondsperday=86400;  
 $firstdayofyear=mktime(12,0,0,1,1,$an);  
 $lastdayofyear=mktime(12,0,0,12,31,$an);  
 $theday = $firstdayofyear;  
 for($theday=$firstdayofyear; $theday<=$lastdayofyear; $theday+=$secondsperday) {  
   $dayinfo=getdate($theday);  
   $jour_actu=date('d',$theday);  
   if ($jour_actu-10<0 ) $jour_actu = substr($jour_actu, strpos($jour_actu, "0") + 1);  
      $mois_actu=date('m',$theday);  
       if ($mois_actu-10<0 ) $mois_actu = substr($mois_actu, strpos($mois_actu, "0") + 1);          
      echo "Le ".$tabjour[$dayinfo['wday']]." ".$jour_actu." ".$tabmois[$mois_actu]." ".$an."\n"; // Jours et mois  
 }  

Pour éclater une chaîne qui a des retour chariot dans un tableau

$array = preg_split("/\r\n|\n|\r/", $string);

Références :

https://stackoverflow.com/questions/1483497/how-to-put-string-in-array-split-by-new-line
href= »https://stackoverflow.com/questions/5696412/how-to-get-a-substring-between-two-strings-in-php

https://stackoverflow.com/questions/4343596/how-can-i-parse-a-json-file-with-php

Articles récents
Commentaires récents
fatima dans Bienvenue !
AdminDroid dans Bienvenue !
fatima dans Bienvenue !
Archives
Catégories
%d blogueurs aiment cette page :