Outils pour utilisateurs

Outils du site


tips_informatiques:programmation:php:code

Ceci est une ancienne révision du document !


startWith

    /**
     * Tests if a string starts with a given string
     *
     * @param     string
     * @param     string
     * @return    bool
     */
    function startWith($string, $start)
    {
        return strpos($string, $start) === 0;
    }

endWith

    /**
     * Tests if a string ends with the given string
     *
     * @param     string
     * @param     string
     * @return    bool
     */
    function endWith($string, $end)
    {
        return strrpos($string, $end) === strlen($string) - strlen($end);
    }

debug

function debug($object)
{
    echo '<div>';
 
    $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);
    }
    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>';
}

to_db_date

/**
* 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))
    {
        return date('Y-m-d H:i:s', $date);
    }
    else
    {
        return null;
    }
}

from_db_date

/**
* Converts a datetime value (as retrieved from the database) to 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;
    }
}

get_value_between_chars

/**
 * 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)
        {
            $cl_pos = strpos($haystack, $closing_char, $op_pos + 1);
 
            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;
        }
    }
 
    if($found)
    {
        return $value;
    }
    else
    {
        return null;
    }
}
tips_informatiques/programmation/php/code.1248076385.txt.gz · Dernière modification: 2009/07/20 00:00 (modification externe)