Functions
WebDev/*nix assorted functions & hacks
Search
find all
Array
-
Bash
-
Classes
-
Config
-
DateTime
-
Debug
-
dotjs
-
File
-
foooo
-
Git
-
HTML
-
Images
-
Javascript
-
Linux
-
Math
-
Misc
-
MySQL
-
OS X
-
Reading
-
Redis
-
Shell scripts
-
snippets
-
SQL
-
Strings
-
TextMate
-
Web
-
\(^o^)/ Fun!
-
_Misc hacks
-
_Pages
Shell scripts
* Readme
dict
edit_json (php)
encrypt/decrypt
pdoc
pftp - ftp a file to some place
php fork & IPC
php input cli
phplint
~/bin/inc/lib.php
10
found
auth
Name:
Category: Shell scripts
Body:
<?php /** * Supporting cast for PHP shell scripts * Some of these will return arrays with first a success boolean, then the return value or error msg * * fetch_url($url, $options = '') * html_decode($string) * interpret_argv() * str_split($str,$num = '1') * freadall() * term_echo($string, $lead=' ') * nl2unix($string), nl2mac($string), nl2dos($string) * * * Updated: * 2004-10-15 * 2005-08-31: added <, > to htmldecode() **/ #____________________________, # fetch_url($url, $options) / v1.1 # Uses Curl library to return data from URL # Returns array(status, body, headers) or array(false, curl's reason) function fetch_url($url, $options = '') { $ch = curl_init ($url); curl_setopt ($ch, CURLOPT_HEADER, 1); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_USERAGENT, "Curlzilla"); if (is_array($options)) { foreach($options as $key => $value) { curl_setopt ($ch, constant($key), $value); } } $res = curl_exec($ch); $error = curl_error($ch); curl_close ($ch); if ($res) { $body = substr(strstr($res, "\r\n\r\n"), 4); $headers = substr($res, 0, strpos($res, "\r\n\r\n")); $status = explode(' ', substr($headers, 0, strpos($headers, "\n"))); $status = (int) $status[1]; return array($status, $body, $headers); }else{ return array(false, "Curl error: ". $error); } } #_______________________, # html_decode($string) / needs Lots of extra key=value pairs function html_decode($string) { # html_entity_decode() does not work in a CLI environment, which makes sense because # lotsa characters just don't work in the terminal. $codex = array( ' '=>' ', '·'=>'*', '<'=>'<', '>'=>'>' ); return str_replace(array_keys($codex), array_values($codex), $string); } #___________________, # interpret_argv() / # returns array with two arrays of the given arguments, # those without and those with leading '-' (files and options) function interpret_argv() { $argv = $GLOBALS['argv']; if (count($argv) == 1) return false; # removing first item => scriptname array_shift($argv); $options = array(); $files = array(); foreach($argv as $arg) { if (strpos($arg, '-') === false) { $files[] = $arg; }else{ $options[] = $arg; } } return array($files, $options); } #_____________________________, if ! function_exists .. # str_split($str,$num = '1') / if (! function_exists('str_split')) { # PHP 5 function str_split($str,$num = '1') { if($num < 1) return FALSE; $arr = array(); for ($i = 0; $i < strlen($str); $i += $num) { $arr[] = substr($str,$i,$num); } return $arr; } } #_____________, # freadall() / function freadall() { $contents = ''; while (!feof(STDIN)) { $contents .= fread(STDIN, 8192); if (! $contents) return false; } return $contents; } #_________________________________, # term_echo($string, $lead=' ') / updated 2004-10-12 function term_echo($string, $lead=' ') { $string = trim(wordwrap($string, 80-strlen($lead))); $arr = explode("\n", $string); echo $lead. join("\n".$lead, $arr) ."\n"; } #___________________, # nl2unix($string) / function nl2unix($string) { return str_replace(array("\r\n", "\r"), "\n", $string); } #__________________, # nl2mac($string) / function nl2mac($string) { return str_replace(array("\r\n", "\n"), "\r", $string); } #__________________, # nl2dos($string) / function nl2dos($string) { return str_replace(array("\r", "\n\n", "\n"), array("\n", "\n", "\r\n"), $string); } if (basename($_SERVER['PHP_SELF']) == 'php.inc') { $funcs = get_defined_functions(); echo " Functions:\n ". join("\n ", $funcs['user']) ."\n"; echo " File parsed OK\n"; } ?>
Footer