3 # This program is in the public domain within the United States. Additionally,
4 # we waive copyright and related rights in the work worldwide through the CC0
5 # 1.0 Universal public domain dedication, which can be found at
6 # http://creativecommons.org/publicdomain/zero/1.0/
9 # pass an integer, returns the suffix
10 function ordinal_suffix($i) {
12 if(strlen($i) > 1 && substr($i, -2, 1) === '1') {
15 switch(substr($i, -1)) {
29 # ordinalize(1) => "1st"
30 # ordinalize(2) => "2nd"
31 # ordinalize(3) => "3rd"
32 # ordinalize(111) => "111th"
33 # ordinalize("62") => "62nd"
34 function ordinalize($i) {
35 return $i . ordinal_suffix($i);
38 # remove the $key from $_REQUEST and return it's value (or null if it's not there)
39 function _REQUEST_cut($key) {
40 if(!isset($_REQUEST[$key])) {
43 $ret = $_REQUEST[$key];
44 unset($_REQUEST[$key]);
48 # returns an array containing just the elements of $pipes that are readable (without blocking)
49 # timeout 0 means don't wait, timeout NULL means wait indefinitely
50 function readable_sockets($pipes, $timeout = 0){
51 $read = array_values($pipes);
52 $ret = stream_select($read, $write = NULL, $exceptions = NULL, $timeout);
70 function exec_pipe($command, $stdin) {
71 $descriptorspec = array(
72 0 => array('pipe', 'r'), // stdin is a pipe that the child will read from
73 1 => array('pipe', 'w'), // stdout is a pipe that the child will write to
74 2 => array('file', '/dev/null', 'w') // stderr is a pipe that the child will write to
77 $process = proc_open($command, $descriptorspec, $pipes);
79 if (is_resource($process)) {
80 fwrite($pipes[0], $stdin);
83 while (!feof($pipes[1])) {
84 $chunk = fread($pipes[1], 1024);
91 // It is important that you close any pipes before calling
92 // proc_close in order to avoid a deadlock
93 $return_value = proc_close($process);
95 return array($return_value, $stdout);
100 function unix_newlines($str) {
101 $str = str_replace("\r\n", "\n", $str);
102 return str_replace("\r", "\n", $str);
106 # return current year (all 4 digits)
107 function this_year() {
108 return strftime('%Y');
111 # return the number of the current month (1..12)
112 function this_month() {
113 return preg_replace('|^0|', '', strftime('%m'));
116 # return today's date in yyyy-mm-dd format
117 function today_ymd() {
118 return strftime('%Y-%m-%d');
121 function now_yyyymmdd() {
122 return strftime('%Y-%m-%d');
125 function now_yyyymmddhhmmss() {
126 return strftime('%Y-%m-%d %H:%M:%S');
130 function get_text_between($text, $start_text, $end_text) {
131 $start = strpos($text, $start_text);
132 if($start === false) {
135 $text = substr($text, $start + strlen($start_text));
136 $end = strpos($text, $end_text);
140 return substr($text, 0, $end);
143 # Make it easy to insert an array into the template data structure so that each
144 # element of the array gets its own row.
146 # passed this: columnize(array('a', 'b', 'c'), 'k');
147 # it returns: array(array('k' => 'a'),
149 # array('k' => 'c'));
150 # passed this: columnate(array(), 'k');
152 function columnize($arr, $key = 'data') {
157 foreach($arr as $val) {
158 $ret[] = array($key => $val);
163 # php4 is broken, in that you cannot set a default value for a parameter that
164 # is passed by reference. So, this is set up to use the following screwy
167 # function foo($bar = 0) {
177 class stupid_reference {
179 function stupid_reference(&$ref) {
183 function ref(&$foo) {
184 return new stupid_reference($foo);
187 function &last(&$array) {
188 if(count($array)) return $array[count($array) - 1];