view includes/class.textutils.php @ 64:d4f26e9767bf

Filtrage via Akismet du spam. Suite aux nombreuses attaques de spammers sur le blog, j'ai du chercher une solution efficace permettant d'y faire face. La solution la plus efficace que j'ai pour le moment, c'est Akismet, qui semble ne pas avoir trop de faux positifs (je n'en ai pas encore eu un seul). Il est necessaire de creer un compte sur WordPress.com pour obtenir une clef permettant d'utiliser le service, c'est totalement gratuit.
author Franck Deroche <webmaster@defr.org>
date Tue, 11 Mar 2008 08:16:11 -0700
parents ec0c926a78a6
children
line wrap: on
line source
<?php
    class TextUtils
    {
	public static function SplitTags($str)
	{
	    $str = str_replace("\\\"", "\"", $str);
	    $tags = array();
	    preg_match_all('/"([a-zA-Z0-9 ]+)"/', $str, $tests);
	    $useful = $tests[1];
	    foreach($useful as $match)
	    {
		$tags[] = $match;
		$str = str_replace("\"{$match}\"", null, $str);
	    }
	    $str = preg_replace("/( )+/", " ", trim($str));
	    if(strlen($str) > 0)
		$tags = array_merge($tags, explode(" ", $str));
	    asort($tags);
	    return $tags;
	}
	
	public static function DiffTagLine($old, $new, &$ajout, &$suppr)
	{
	    $arOld = TextUtils::SplitTags($old);
	    $arNew = TextUtils::SplitTags($new);
	    $ajout = array_diff($arNew, $arOld);
	    $suppr = array_diff($arOld, $arNew);
	}

    public static function StripTitle($Titre) {
        $Titre_url = str_replace(" ", "_", strip_tags($Titre));
		$Titre_url = str_replace("-", "_", $Titre_url);
        $Titre_url = str_replace(array("é", "è"), "e", $Titre_url);
        $Titre_url = str_replace("à", "a", $Titre_url);
        $Titre_url = str_replace("ù", "u", $Titre_url);
        $Titre_url = str_replace(array("î", "ï"), "i", $Titre_url);
        return $Titre_url;
    }

    public static function getMonthName($monthNumber) {
        return ucfirst(strftime('%B', strtotime('2007-' . $monthNumber . '-01')));
    }

    public static function EnsureUTF8($string) {
        return TextUtils::SeemsUTF8($string) ? $string : utf8_encode($string);
    }

    public static function SeemsUTF8($Str) {
        for ($i=0; $i<strlen($Str)/10 || $i < 100; $i++) {
            if (ord($Str[$i]) < 0x80) continue; # 0bbbbbbb
            elseif ((ord($Str[$i]) & 0xE0) == 0xC0) $n=1; # 110bbbbb
            elseif ((ord($Str[$i]) & 0xF0) == 0xE0) $n=2; # 1110bbbb
            elseif ((ord($Str[$i]) & 0xF8) == 0xF0) $n=3; # 11110bbb
            elseif ((ord($Str[$i]) & 0xFC) == 0xF8) $n=4; # 111110bb
            elseif ((ord($Str[$i]) & 0xFE) == 0xFC) $n=5; # 1111110b
            else return false; # Does not match any model
            for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
                if ((++$i == strlen($Str)) || ((ord($Str[$i]) & 0xC0) != 0x80))
                    return false;
	        }
        }
        return true;
    }
    }
?>