PHP
Basics
PHP Variables
$DOCUMENT_ROOT $PHP_SELF $_GET['url_param'] $_POST['form_param'] $my_param = htmlentities($_POST['form_param']);
Check variables
is_numeric() is_file() is_dir()
Strings
Usefull stuff
strtolower strtoupper trim // trims spaces at the ends of string nl2br // \n ->
// Search in string ereg[i](was,wo); // i: case insensitive // Replace chars ereg[i]_replace (was,womit,wo); // i: case insensitive str_replace(was,womit,wo); // case sensitive, works on arrays as well // Pos of substring strpos ($file_name,"."[,startint]); // start from left strrpos($file_name,"."[,startint]); // start from right
// Part of string $ext = strrchr($file_name,"."); // start from right and take all up to the end $filename = str[i]str($file_name,"."); // start from left, i: case insensitive // Return a substring substr($string, pos [, number]); // pos can be negative
Work on special chars
stripslashes // removes \ addslashes // adds \ to " \ NULL quotemeta // adds \ to . \\ + * ? [ ] ^ ( ) $ htmlentities // converts into HTML chars // Umlaute: utf8 -> htmlcode $text = htmlentities(utf8_decode($text));
Arrays
Split string to array
explode(" und ",$str); split[i](" UnD ",$Str); // i: case insensitive
Array to string
implode(", ",$moin);
Sort
sort($array); natsort($array); // 2 before 10
Remove items
unset($array[$x]); // nicer: $raus = array(); $raus[] = $array[$x]; $array = array_diff($array,$raus);
while (($id = array_search($what,$where))>-1) unset ($files[$id]);
Remove duplicates
$a = array_values(array_unique($a));
Compare arrays
$c = array_diff ($a, $b); $c = array_intersect ($a, $b); $c = array_merge ($a, $b);
Print a hash
foreach ($array as $key=>$val) echo "key = $key, val = $val";
Regular Expressions
Special chars
. = 1 char .? = 0,1 chars .* = 0-oo chars .+ = 1-oo chars
\s = whitespace \S = no whitespace \w = word char (=a-z,0-9,_) \W = no word char \d = decimal number \D = no decimal number \b = word boundary \B = no word boundary \n = new line (Linux) \r = CR (Windows: \n\r)
Options
i = case insensitive e = execute php code inside expression s = single string, (search passes lineends) U = ungreegy
Search
eregi('EXPRESSION', $string) preg_match("'EXPRESSION'is", $where, $results); preg_match_all("'EXPRESSION'is", $where, $results); $result1 = $results[1]; //Check eMail-Syntax
Replace
preg_replace($patterns, $replacements, $where); preg_replace("'<title>[^<]*</title>'is","",$htmlinhalt);
$was = array(); $womit = array(); .. $text = preg_replace($was,$womit,$text);
Other Stuff
$date = date("Y-m-d"); // Date to mysql format
Run External Command
exec("command"); //waits until command is finnished exec("command >/dev/null 2>&1"); // ignores output, so php continues right away
Images
Read Image Size
$array = getimagesize($file);
Char Encoding
[1] The simplest way to handle this problem is to send the encoding yourself, via your programming language. The appropriate code is:
header('Content-Type:text/html; charset=UTF-8');
This code must come before any output, so be careful about stray whitespace in your application (i.e., any whitespace before output excluding whitespace within <?php ?> tags).
Encode spaces etc from a string for usage as url-paramter:
$file = rawurlencode ($file); $file = rawurldecode ($file);
Functions
function delTree($dir) { $files = glob( $dir . '*', GLOB_MARK ); foreach( $files as $file ){ if( is_dir( $file ) ) delTree( $file ); else unlink( $file ); } if (is_dir($dir)) rmdir( $dir ); } function recurse_chown_chgrp($mypath, $uid, $gid) { $d = opendir ($mypath) ; while(($file = readdir($d)) !== false) { if ($file != "." && $file != "..") { $typepath = $mypath . "/" . $file ; if (filetype ($typepath) == 'dir') { recurse_chown_chgrp ($typepath, $uid, $gid); } chown($typepath, $uid); chgrp($typepath, $gid); } } } function recurse_chmod($mypath, $mod) { $d = opendir ($mypath) ; while(($file = readdir($d)) !== false) { if ($file != "." && $file != "..") { $typepath = $mypath . "/" . $file ; if (filetype ($typepath) == 'dir') { recurse_chmod ($typepath, $mod); } chmod($typepath, $mod); } } }
Login
function auth() { header('WWW-authenticate: basic realm="Name of realm"'); header('HTTP/1.0 401 Unauthorized'); exit("text..."); } // username / passwd in $PHP_AUTH_USER / $PHP_AUTH_PW if(!isset($PHP_AUTH_USER)) auth(); else { // check if $PHP_AUTH_USER / $PHP_AUTH_PW are fine. If not run auth() }