Tutorial Count Script Excecution Time
$execution_time = microtime(); # Start counting
# Your code
$execution_time = microtime() - $execution_time;
$execution_time = sprintf('It took %.5f sec', $execution_time);
$execution_time = microtime(); # Start counting
# Your code
$execution_time = microtime() - $execution_time;
$execution_time = sprintf('It took %.5f sec', $execution_time);
Give function hex code (e.g. #eeeeee), returns array of RGB values.
function hex2rgb( $colour ) {
if ( $colour[0] == '#' ) {
$colour = substr( $colour, 1 );
}
if ( strlen( $colour ) == 6 ) {
list( $r, $g, $b ) = array( $colour[0] . $colour[1], $colour[2] . $colour[3], $colour[4] . $colour[5] );
} elseif ( strlen( $colour ) == 3 ) {
list( $r, $g, $b ) = array( $colour[0] . $colour[0], $colour[1] . $colour[1], $colour[2] . $colour[2] );
} else {
return false;
}
$r = hexdec( $r );
$g = hexdec( $g );
$b = hexdec( $b );
return array( 'red' => $r, 'green' => $g, 'blue' => $b );
}
Easy way to turn a CSV file into a parseable array.
<?php
$str="foo,bar,baz,bat";
$arr=explode(",",$str);
// print_r($arr);
?>
function br2newline( $input ) {
$out = str_replace( "<br>", "\n", $input );
$out = str_replace( "<br/>", "\n", $out );
$out = str_replace( "<br />", "\n", $out );
$out = str_replace( "<BR>", "\n", $out );
$out = str_replace( "<BR/>", "\n", $out );
$out = str_replace( "<BR />", "\n", $out );
return $out;
}
Converts a break tag to a newline - no matter what kind of HTML is being processed.
function br2nl( $input ) {
return preg_replace('/<br(\s+)?\/?>/i', "\n", $input);
}
For instance, if you want to use a string as part of a URL but need to make it safe for that kind of use.
function replace_accents($str) {
$str = htmlentities($str, ENT_COMPAT, "UTF-8");
$str = preg_replace('/&([a-zA-Z])(uml|acute|grave|circ|tilde);/','$1',$str);
return html_entity_decode($str);
}
<?php
/*
This code by Chris Coyier
*/
$i = 0; // counter to be used later;
?>
Comments in PHP can be between /* */ markings (useful for multiple line comments) or after // markings (for single lines only).
Variables that are submitted via web forms always need to be cleaned/sanitized before use in any way, to prevent against all kinds of different malicious intent.
function clean($value) {
// If magic quotes not turned on add slashes.
if(!get_magic_quotes_gpc())
// Adds the slashes.
{ $value = addslashes($value); }
// Strip any tags from the value.
$value = strip_tags($value);
// Return the value out of the function.
return $value;
}
$sample = "<a href='#'>test</a>";
$sample = clean($sample);
echo $sample;