De-reffing: $url = ${'url' . $site}; I.e. $url = $url3 if $site == 3; or: $s = 'url' . $site; $url = ${$s}; function myprintvar ($name) { //echo ('

'); echo ($name . ': ' . $GLOBALS[$name]); echo ("
\n"); } trim, clean, chop: $str = trim($str); // 2nd one is interesting and works; one above simpler. function myprintvar ($name) { //echo ('

'); eval("global \${$name};"); echo ($name . ': ' . ${$name}); echo ("
\n"); } print_r($array); The key debugging print. If printed to html, precede by
function myprint_r ($a) { //echo ('

'); echo ('
'); print_r($a); echo ('

'); }

// conditional controlling plain HTML A is equal to 5 Z is something else $imin = ($varoldrowlabels ? 0 : 1); // alt. to if-then // var. nmb of fn-args. varargs / VARARGS in PHP =>Can get the args as an array; or can name them but test for existence before using/reffing them. function fn($a, $b, $c, $d, $e, $dopost) { $arr = func_get_args(); foreach ($arr as $x) { echo('x: '. $x . "
\n"); }; if (! $dopost) { $dopost = 2; }; // default value }

Include file "text"

// SHOW EVERYTHING of what php server has/knows //foreach ($GLOBALS as $key => $val) { echo "$key => $val
"; }; //myprintvar('vardata1'); //myprintvar('php_errmsg'); //echo('GET '); myprint_r($GLOBALS['_GET']); //echo('POST '); myprint_r($GLOBALS['_POST']); //echo('POST2 '); myprint_r($_POST); //echo('REQUEST '); myprint_r($_REQUEST); //foreach ($GLOBALS['_GET'] as $key => $val) { echo "$key => $val
"; }; //foreach ($GLOBALS as $key => $val) { echo "$key => $val
"; }; // PART 1: SETTING PARAMETERS, INCOMING VARS. continue / break (not: next, last) For loop: for ($i = strlen($findstr); $i >= 1; $i--) { }; function mytrim(&$val) {$val = trim($val);}; array_walk($menu2, 'mytrim'); $menu2 = array_map('trim', $menu2); map is simpler, but dups array. walk does it in place. //error_reporting(E_ALL); echo "phpError: $php_errormsg\n"; Can use globals in functions AND set them there: like static vars. global ${$varname}; $currentval = ${$varname}; FETCHING FILES file_get_contents('text') // file file_get_contents('http://www.psy.gla.ac.uk/index.html') // webpage file_get_contents('http://www.psy.gla.ac.uk/index.html') // remote webpage Must replace spaces in filenames by %20, at least for URLs: $dfile2 = str_replace(' ', '%20', $dfile); $buf = file_get_contents(mkurl($dfile2) . '/'); SORTING function cmp($a, $b) { $a = strrev($a); $b = strrev($b); return ($a == $b ? 0 : ($a > $b)); }; function testandprint($arr) { //sort($arr); $arr = array_unique($arr); // uniq ! usort($arr, 'cmp'); /// sort by last domain, not first. .... } STRINGS $a == $b tests for eq of strings (diff. objects, but same string) $arr[] = substr_replace($big , $bit, $pos, len); substr_replace replaces len (or 0) chars at pos by small or empty string inside big one. $out = str_replace('TGT', $url, $orig); no REs. Always does global replace $string[3] = 'a' replaces char 4 in a string. pos, bit strpos($haystack, $needle) -> pos of where substring first matches. (there are nocase versions, strstr($haystack, $needle, $bool-before) finds where start of (1st instance of) needle is; splits haystack -> Returns half of haystack after/before that pt. nocase version too. substr_count($haystack, $needle) -> nmb. of non-overlapping instances of needle ob_flush(); flush(); ob_flush(); flush(); ob_flush(); flush(); // however these still didn't progressively flush a big page SED LIKE EDITS Always use preg_match/replace, not ereg. However with preg, the pattern must include delimiters e.g. // or ;; s/abc/xyz/is == preg_replace('/abc/is', 'xyz', $buf, 1); s/abc/xyz/g == preg_replace('/abc/', 'xyz', $buf); (global v.1) $buf = preg_replace("|\n\s*#[^\n]*|s", "", $buf); (global v.2) Above does all by not eating next LF; unlike next: $buf = preg_replace("|\n\s*#.*?\n|s", "\n", $buf); BAD (global v.3) The diff is "match greedy while not LF" vs. "match min. to next LF" while ($count) $buf = preg_replace("|\n\s*#.*?\n|s", "\n", $buf, -1, $count); (global v.4) $buf = preg_replace("|\n\s*#.*?(?=\n)|s", "", $buf); (global v.5) Lookahead but don't eat the LF, though takes more chars than v.2 See http://uk3.php.net/manual/en/regexp.reference.assertions.php $obuf = preg_replace('||i', "\${0}$s3", $obuf, 1); Note \ to stop $0 being interpreted before RE; and {} in case s3 has a leading digit. $c1 = preg_replace('/=(.*);/e', ' \'=\' . base64_decode(${1}) . ";";', $c1); Note a) with /e replace string must be a valid line of PHP code. b) trouble with = as a string/char "; ?> // deletes incoming up to first
. // Has to use non-greedy AND limit match to 1 echo preg_replace('/.*?
/s', '
', file_get_contents($urlroot . 'printchris2.php'), 1); // deletes (URL) string after 3rd slash. $targetbase = preg_replace('|^((.*?/){3}).*|s', '${1}', $target, 1); a{5} = 'aaaaa'; abc{3} = 'abcabcabc'; \d{4,6} = any nmb. 4..6 digits long. '((.*?str){3})' = all text from the start up to and including the 3rd occurrence of 'str'. Replacing the 3rd occurrence probably needs something like: '((str.*?){2})str' and replacing $1 plus append the new text. if (preg_match('/,del/', $f)) continue; // deleted pmwiki pages if (preg_match("|($pat)|", $buf, $matches)) { $link = $matches[1]; // I.e. must have $matches, and $1-> $m[1] if (preg_match('/<\?php/is', $array[$k])) { while (preg_match('/<\?php.*$1', $array[$k]); // Add PRE iff multi-line php $array[$k] = preg_replace('/<\?php/is', '<?php', $array[$k]); }; if (file_exists($dir . $f)) $stoparray[$f] = 1; is_writable() //$a = array_merge($stoplist, $mustlist); //foreach ($stoparray as $key => $val) { echo "$key => $val
"; }; //foreach (array_keys($array) as $key) { echo "$key
"; }; $array = array_keys($_REQUEST); $who = $array[0]; // gets request and uses only first key, no value. Don't know in fact what key you'll get back first if more than one. //if ($f && !preg_match("/.html$/", $f)) { $f = "$f.html"; }; //exec("ls $pattern", $plist); // echo, but split to 1 per line }; }; SPLIT() split() $parray = array(); exec("echo $pattern", $parray); $pstr = implode(' ', $parray); // all in a string of joined lines $parray = preg_split('/\s+/', $pstr); //$parray = explode(' ', $pstr); //foreach ($stoparray as $key => $val) { echo "$key => $val
"; };

UNIX callouts

system(' ./wtable < calendar.tab 2>&1'); also works // see DOCphp for more $path_parts = pathinfo('/www/htdocs/index.html'); echo $path_parts['dirname'], "\n"; dir part echo $path_parts['basename'], "\n"; filename, no dir, with extension == basename($file) echo $path_parts['extension'], "\n"; Stuff on rel->abs URLs You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself: Main text last changed ............. Length: //Script date/time: Last regenerated on // time right now // time this script was last modified. Last explicitly updated on Length: Does 1,000 commas; does rounding to 2 sigFigs here.