Ceci est une ancienne révision du document !
−Table des matières
PHP Tips & Tricks
DateTime
Php => Mysql => Php
$mysqldate = date( 'Y-m-d H:i:s', $phpdate ); $phpdate = strtotime( $mysqldate );
strftime
le paramètre %e pour mettre le jour du mois sur 1 seul chiffre (1 à 31) ne fonctionne pas avec Php/Windows. La raison est que strftime n'est pas portable et la doc php n'est donc pas correcte pour tous les OS.
Voir:
- le bug et sa solution: http://bugs.php.net/bug.php?id=45847
- La bonne doc à lire est donc strftime windows documentation: http://msdn.microsoft.com/en-us/library/fe06s4ak%28VS.71%29.aspx
La solution pour le %e : remplacer par “%#d”
Gestion des erreurs
Exception
Sécurité
Strong cryptography in PHP: Use standard algorithms, Key space, Kerchoof’s principle, Don’t use rand() or mt_rand(), Use a salt value in hash functions, Size and strength of the passwords, Don’t use plaintext passwords as key for ciphers, Use Base64 to encode encrypted data.
Divers
DocComment & ReflectionClass
Une démonstration de la récupération des commentaires Advanced Documentation With Reflection In PHP 5, mais obsolète car utilise la fonction php_check_syntax() qui n'est plus dans php depuis php 5.0.4
phpDocumentor home page
See the example at de3.php.net/manual/en/reflectionclass.getdoccomment.php - if you want to parse files for classes, you can use github.com/theseer/Autoload/blob/master/src/classfinder.php and to iterate over directories you use de3.php.net/manual/en/class.recursivedirectoryiterator.php
RegEx
Validation syntaxe adresse email :
function mail_checksyntax( $email ) { if( isset($email) && eregi('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$', $email) ) { return true ; } return false ; }
Voir aussi Vos applications valident-elles correctement les adresses e-mail ?, Mail::RFC822::Address: regexp-based address validation
Cross Platform
if (strtoupper(substr(PHP_OS, 0, 3) == 'WIN')) { $s_eol = "\r\n"; } elseif (strtoupper(substr(PHP_OS, 0, 3) == 'MAC')) { $s_eol = "\r"; } else { $s_eol = "\n"; }
XML
A introductory tutorial on simplexml can be found here:
XSLT
<xsl:text disable-output-escaping="yes"> &nbsp;</xsl:text>
fonctionne mieux que CDATA …
Pointeur de fonction
Singleton
Manipulation fichier Excel
Introduction à PHPExcel par Ernaelsten Gérard (05/02/2009)
Le package Spreadsheet_Excel_Writer
/usr/local/php5/bin/pear -d preferred_state=beta install OLE /usr/local/php5/bin/pear -d preferred_state=beta install Spreadsheet_Excel_Writer
Mysqlnd
mysqlnd plugins for PHP in practice: example on how to write a plugin for mysqlnd.
PHP: Transparent load balancing and sharding with mysqlnd: You want some client-side MySQL load balancing with and without sharding for your PHP application? PHP 5.3 has something to offer for you. It is free. It requires no to very little changes to your applications.