Php > fonctions récurrentes

Titrefonctions récurrentes
Postée le09-09-2011
Affichée192
Mini-lien
Description

quelques méthodes statiques que j'utilise dans la plupart de mes projets

EtatNe contient pas d'erreurs. Ne contient pas d'erreurs.
Code d'insertion
Options
Afficher les numéros de lignes  Mettre la source en plein ecran  Selectionner la source  Partager sur Facebook 
Téléchargement Telecharger en format txt  Telecharger en format pdf  Telecharger en format php
Plein ecran
class useful {
       
        static public function parseUrl($url) {
                $urlregex = "`^(https?)\:\/\/([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)*(\:[0-9]{2,5})?(\/([a-z0-9+\$_-]\.?)+)*\/?(\?[a-z+&\$_.-][a-z0-9;:@/&%=+\$_.-]*)?(#[a-z_.-][a-z0-9+\$_.-]*)?\$`i";
                $pregmatch = preg_match($urlregex, $url);
                $value = ($pregmatch != 1) ? false : true;
                return $value;
        }
       
        static public function truncate($string, $length = null) {
                if(empty($string)) return false;
                if(null === $length) $length = TRUNCATE_LENGTH;
                if(strlen($string) <= TRUNCATE_LENGTH) {
                        $truncated_string = $string;
                }
                else {
                        $truncated_string = substr($string, 0, strrpos(substr($string, 0, TRUNCATE_LENGTH), ' '));
                        $punct = array(',', ';', '.', ':', '!', '?', '-', '|');
                        while (in_array(substr($truncated_string, -1, 1), $punct)) {
                                $truncated_string = substr($truncated_string, 0, -1);
                        }
                        $truncated_string = $truncated_string . '...';
                }
                return $truncated_string;
        }
       
        static public function setPassword() {
                $pwd['upper'] = 'ABCDEFGHJKLMNPQRSTUVWXYZ';
                $pwd['lower'] = 'abcdefghijkmnopqrstuvwxyz';
                $pwd['number'] = '123456789';
                $pwd['sign'] = '.$&-#§*';
                $password = '';
                foreach ($pwd as &$part) {
                        $part = str_shuffle($part);
                        $password .= substr($part, 0, 2);
                }
                $password = str_shuffle($password);
                return $password;
        }
       
}