Outils pour utilisateurs

Outils du site


tips_informatiques:programmation:php:code

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
tips_informatiques:programmation:php:code [2009/07/13 12:11]
nico
tips_informatiques:programmation:php:code [2009/08/12 00:00] (Version actuelle)
Ligne 1: Ligne 1:
-====== ​function ​startWith ======+====== startWith ======
 <code php> <code php>
     /**     /**
Ligne 14: Ligne 14:
 </​code>​ </​code>​
  
-====== ​function ​endWith ======+====== endWith ======
 <code php> <code php>
     /**     /**
Ligne 29: Ligne 29:
 </​code>​ </​code>​
  
-====== ​function ​debug ======+====== debug ======
 <code php> <code php>
-function debug($object)+class DebugTool
 { {
-    ​echo '<​div>';​ +    ​public static function show($object, $title ​null, $backtrace_index = 0)
-     +
-    ​$calledFrom ​debug_backtrace();​ +
-    echo '<​strong>'​ . $calledFrom[0]['​file'​] . '</​strong>';​ +
-    echo ' (line <​strong>'​ . $calledFrom[0]['​line'​] . '</​strong>​)';​ +
-  +
-    echo('<​pre>'​);​ +
-    if(is_array($object))+
     {     {
-        print_r($object);​+        ​echo '<​div class="​debug">';​ 
 +       
 +        $calledFrom = debug_backtrace();​ 
 +        echo '<​strong>'​ . $calledFrom[$backtrace_index]['​file'​] . '</​strong>';​ 
 +        echo ' (line <​strong>'​ . $calledFrom[$backtrace_index]['​line'​] . '</​strong>​)';​ 
 +      
 +        if(isset($title)) 
 +        { 
 +            echo '<​h3>'​ . $title . '</​h3>';​ 
 +        } 
 +         
 +        echo('<​pre>'​);​ 
 +        if(is_array($object)) 
 +        { 
 +            ​print_r($object)
 +        } 
 +        elseif(is_a($object,​ '​DOMDocument'​)) 
 +        { 
 +            $object->​formatOutput = true; 
 +            $xml_string = $object->​saveXML();​ 
 +            echo htmlentities($xml_string);​ 
 +        } 
 +        elseif(is_a($object,​ '​DOMNodeList'​) || is_a($object,​ '​DOMElement'​)) 
 +        { 
 +            $dom = new DOMDocument();​ 
 +            $debugElement = $dom->​createElement('​debug'​);​ 
 +            $dom->​appendChild($debugElement);​ 
 +             
 +            if(is_a($object,​ '​DOMNodeList'​)) 
 +            { 
 +                foreach ($object as $node)  
 +                { 
 +                $node = $dom->​importNode($node,​ true); 
 +                $debugElement->​appendChild($node);​ 
 +                } 
 +            } 
 +            elseif(is_a($object,​ '​DOMElement'​)) 
 +            { 
 +                $node = $dom->​importNode($object,​ true); 
 +                $debugElement->​appendChild($node);​ 
 +            } 
 +             
 +            $dom->​formatOutput = true; 
 +            $xml_string = $dom->​saveXML();​ 
 +            echo htmlentities($xml_string);​ 
 +        } 
 +        elseif(is_object($object)) 
 +        { 
 +            echo get_class($object);​ 
 +        } 
 +        else  
 +        { 
 +            echo $object; 
 +        } 
 +         
 +        echo('</​pre>'​);​ 
 +        echo '</​div>'​;
     }     }
-    elseif(is_a($object, ​'​DOMDocument'​))+}  
 + 
 +/​*******************************************************************/​ 
 +/***** GLOBAL FUNCTIONS ********************************************/​ 
 +/​*******************************************************************/​ 
 + 
 +function debug($object, $title = null) 
 +
 +    DebugTool :: show($object, ​$title, 1); 
 +
 +</​code>​ 
 + 
 +====== to_db_date ====== 
 +<code php> 
 +/** 
 +* Converts a UNIX timestamp (as returned by time()) to a datetime string 
 +* for use in SQL queries. 
 +* @param int $date The date as a UNIX timestamp. 
 +* @return string The date in datetime format. 
 +*/ 
 +static function to_db_date($date) 
 +
 +    if (isset($date))
     {     {
-        ​$object->​formatOutput = true; +        ​return date('​Y-m-d H:​i:​s', ​$date);
-        $xml_string = $object->​saveXML();​ +
-        echo htmlentities($xml_string);+
     }     }
-    ​elseif(is_a($object,​ '​DOMNodeList'​))+    ​else
     {     {
-        ​$dom new DOMDocument(); +        ​return null; 
-        $debugElement ​$dom->createElement('debug'); +    } 
-        $dom->​appendChild($debugElement)+
-         +</​code>​ 
-        ​foreach ​($object as $node+ 
 +====== from_db_date ====== 
 +<code php> 
 +/** 
 +* Converts a datetime value (as retrieved from the databaseto a UNIX 
 +* timestamp (as returned by time()). 
 +* @param string $date The date as a UNIX timestamp. 
 +* @return int The date as a UNIX timestamp. 
 +*/ 
 +static function from_db_date($date) 
 +
 +    if (isset ($date)) 
 +    { 
 +        ​return strtotime($date); 
 +    } 
 +    else 
 +    { 
 +        return null; 
 +    } 
 +
 +</​code>​ 
 + 
 + 
 +====== get_value_between_chars ====== 
 +<code php> 
 +/** 
 + * Return the string found between two characters. If an index is given, it returns the 
 + * value at the index position 
 + *  
 + * @param string $opening_char 
 + * @param string $closing_char 
 + * @param int $index 0 based index 
 + * @return string or null 
 + */ 
 +function get_value_between_chars($haystack, $index = 0, $opening_char = '[', $closing_char = ']') 
 +
 +    $offset = 0
 +    $found = true; 
 +    $value = null; 
 +     
 +    for ($i = 0$i < $index + 1; $i++) 
 +    { 
 +        ​$op_pos = strpos($haystack, ​$opening_char,​ $offset); 
 +        if($op_pos !== false)
         {         {
-        $node $dom->​importNode($nodetrue); +            ​$cl_pos ​strpos($haystack$closing_char,​ $op_pos + 1); 
-         $debugElement->​appendChild($node);+ 
 +            if($cl_pos !== false) 
 +            { 
 +                $value = substr($haystack, $op_pos + 1, $cl_pos - $op_pos - 1)
 +                $offset = $cl_pos + 1; 
 +            } 
 +            else 
 +            { 
 +                $found = false; 
 +                break; 
 +            } 
 +        } 
 +        else 
 +        { 
 +            $found = false; 
 +            break;
         }         }
-        ​ 
-        $dom->​formatOutput = true; 
-        $xml_string = $dom->​saveXML();​ 
-        echo htmlentities($xml_string);​ 
     }     }
-    ​elseif(is_object($object))+    ​ 
 +    if($found)
     {     {
-        ​echo get_class($object);+        ​return ​$value;
     }     }
-    else +    else
     {     {
-        ​echo $object;+        ​return null;
     }     }
-    echo('</​pre>'​);​ 
-      
-    echo '</​div>';​ 
 } }
 </​code>​ </​code>​
 +
 +
 +
 +
 +
 +
 +====== to_multilevel_array ======
 +
 +<code php>
 +/**
 + * Build an array from a list of strings
 + ​* ​
 + * E.g: Array with the following strings:
 + ​* ​
 + * '​general_description[0][0][string]'​
 + * '​general_description[0][1][string]'​
 + * '​general_description[1][0][string]'​
 + ​* ​
 + * @param array $strings Array of (strings => value) pairs to merge into a multilevel array
 + * @param string $opening_char
 + * @param string $closing_char
 + */
 +public static function to_multilevel_array($strings,​ $opening_char = '​[',​ $closing_char = '​]'​)
 +{
 +    $array = array();
 +    ​
 +    foreach ($strings as $string => $value) ​
 +    {
 +        self :: set_next_level_array($array,​ $string, $value, $opening_char,​ $closing_char); ​  
 +    }
 +    ​
 +    return $array;
 +}
 +
 +private static function set_next_level_array(&​$container_array,​ $string, $value, $opening_char = '​[',​ $closing_char = '​]'​) ​   ​
 +{
 +    $key = self :: get_value_between_chars($string,​ 0, $opening_char,​ $closing_char);​
 +    $sub_string = substr($string,​ strpos($string,​ $closing_char) + 1);
 +    ​
 +    if(isset($sub_string) && strlen($sub_string) > 0)
 +    {
 +        if(isset($container_array[$key]))
 +        {
 +            $sub_array = $container_array[$key];​
 +        }
 +        else
 +        {
 +            $sub_array = array();
 +        }
 +        ​
 +        self :: set_next_level_array($sub_array,​ $sub_string,​ $value, $opening_char,​ $closing_char);​
 +    ​
 +        $container_array[$key] = $sub_array;
 +    }
 +    else
 +    {
 +        if(isset($container_array[$key]))
 +        {
 +            $container_array[$key] = array_merge($container_array[$key],​ $value);
 +        }
 +        else
 +        {
 +            $container_array[$key] = $value;
 +        }
 +    }
 +}
 +</​code>​
 +
 +
 +
 +
 +
 +
  
  
tips_informatiques/programmation/php/code.1247479877.txt.gz · Dernière modification: 2009/07/13 00:00 (modification externe)