1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
<?php
/* OpenDb - Open Media Lending Database
Copyright (C) 2001,2002 by Jason Pell
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/**
If strlen($value)==0 will return $ifnull value
instead.
*/
function ifempty($value, $ifnull)
{
if(strlen($value)==0)
return $ifnull;
else
return $value;
}
/**
Returns TRUE if $array is array, AND has at least
one element. I hate this is_array(...) and count(...)
combination which is pissing me off.
*/
function is_not_empty_array($array)
{
if(is_array($array) && count($array)>0)
return TRUE;
else
return FALSE;
}
function is_empty_array($array)
{
return is_empty_or_not_array($array);
}
/**
Returns TRUE if $array is not array, OR has no
elements.
I know you can probably use 'empty($array)', but
considering how some of the functionality of these
functions can change, I don't want to risk it.
*/
function is_empty_or_not_array($array)
{
if(!is_array($array) || count($array)==0)
return TRUE;
else
return FALSE;
}
/**
Stupid strrpos only allows searching on first
character, so this is proper version.
*/
function laststrpos($haystack, $needle)
{
// Initialise.
$index = FALSE;
$idx = strpos($haystack, $needle);
while($idx!==FALSE)
{
$index = $idx;
$idx = strpos($haystack, $needle, $index+strlen($needle));// Move past found needle.
}
return $index;
}
/**
Extended version of str_replace. Even though the doco says that
you can specify arrays for both find and replace, it does not seem
to work properly on the version of PHP I am using. (4.0.4p1), so
I have written my own.
*/
function str_replaces($find_r, $replace_r, $buf)
{
if(strlen($buf)>0)
{
for ($j=0; $j<count($find_r); $j++)
{
if(strlen($find_r[$j])>0)
{
if($replace_r[$j])
$buf = str_replace($find_r[$j], $replace_r[$j], $buf);
else
$buf = str_replace($find_r[$j], "", $buf);
}
}
}
return $buf;
}
/**
My explode function (supports ignoring escaped \$delimiter) which splits
$value into separate array entries, based on $delimiter.
*/
function my_explode($delimiter, $value)
{
$argument='';
for($i=0; $i<strlen($value); $i++)
{
if($value[$i] == $delimiter)
{
if($i>0 && $value[$i-1]=="\\")
{
$argument[strlen($argument)-1] = $value[$i];//Get rid of escape character
}
else
{
$arguments[] = $argument;
$argument='';
}
}
else
{
$argument .= $value[$i];
}
}
if(strlen($argument)>0)
{
$arguments[] = $argument;
}
return $arguments;
}
/**
* Perform explode, but then trim each array
* element before returning.
*/
function trim_explode($delimiter, $value)
{
if(strlen(trim($value))>0)
{
$tmp_values_r = explode($delimiter, $value);
if(is_not_empty_array($tmp_values_r))
{
// we need to trim all the entries
$values_r = NULL;
for($i=0; $i<count($tmp_values_r); $i++)
{
$values_r[] = trim($tmp_values_r[$i]);
}
}
else
{
$values_r[] = trim($value);
}
return $values_r;
}
else
{
return NULL;
}
}
/*
* Check if $s1 startsWith $s2
*
* Where $s1 is smaller than $s2, return FALSE
* Where $s1 same length as $s2, do a direct '==' comparison
* Where $s1 is larger than $s2, then substr to length of
* $s2 and do '==' comparison.
*/
function starts_with($s1, $s2)
{
if(strlen($s1) < strlen($s2))
return FALSE;
else if(strlen($s1) == strlen($s2))
return ($s1 == $s2);
else
return (substr($s1,0,strlen($s2)) == $s2);
}
function ends_with($s1, $s2)
{
if(strlen($s1) < strlen($s2))
return FALSE;
else if(strlen($s1) == strlen($s2))
return ($s1 == $s2);
else
return (substr($s1,-(strlen($s2)),strlen($s2)) == $s2);
}
/**
A pedestrian attempt to trim URL in a neat way.
*/
function trim_url($str, $length)
{
if(strlen($str)>$length)
return substr($str, 0, ($length/2)-3)."...".substr($str, strlen($str)-($length/2));
else
return $str;
}
/*
* This function does not search nested arrays.
*
* @param $strcasecmp Specifies whether to do Case INSENSITIVE comparison
* or not.
*/
function array_search2($needle, $haystack, $strcasecmp=FALSE)
{
if(is_array($haystack))
{
reset($haystack);
while(list($key,$value) = each($haystack))
{
if(($strcasecmp!==TRUE && strcmp($value, $needle)===0) || ($strcasecmp===TRUE && strcasecmp($value, $needle)===0))
return $key;
}
}
//else
return FALSE;
}
// Replace Windows and Mac newlines with Unix standard newline.
function replace_newlines($value)
{
// 1) Replace all '\r\n' with single '\n'
// 2) Replace all remaining '\r' with a '\n'
return str_replace("\r", "\n",
str_replace("\r\n", "\n", $value));
}
/**
Format sql in clause from set of values in array.
Returns the clause minus the IN ( ... )
Does no escaping of ' quotes, so ensure they
are not present in the array_of_values array.
*/
function format_sql_in_clause($array_of_values)
{
$inclause = "";
while(list(,$value) = @each($array_of_values))
{
if(strlen($inclause)>0)
$inclause .= ", ";
$inclause .= "'$value'";
}
if(strlen($inclause)>0)
return $inclause;
else
return NULL;
}
/**
If a title is specified with an article "The", "An", "A", etc
then move it to end of title, with a ',' separator.
Note: Match is NOT case sensitive.
*/
function format_title_grammar_article($title, $articles)
{
while (list(,$article) = each($articles))
{
$article = trim($article);
// If $title starts with $entry - NOT CASE SENSITIVE!!!
if(strcasecmp(substr($title, 0, strlen($article." ")),$article." ")===0)
{
// INITCAP the $entry.
$title = substr($title, strlen($article." "), strlen($title)).", ".
substr($title, 0, strlen($article." "));
// Hit first article, so get out of here.
break;
}
}
return $title;
}
// Utilities
/**
Common formatting function.
@param $lookup_table
Should be an array of the following format: array('code'=>'text')
Where 'code' is the numeric code. If an entry is found in
the lookup table, it will be used instead of the chr(code)
call.
*/
function convert_html_numeric_codes($text, $lookup_table=NULL)
{
$start_of_match = 0;
while(preg_match("/&#([0-9]+);/", substr($text,$start_of_match), $matches))
{
$start_of_match = strpos($text, $matches[0], $start_of_match);
$end_of_match = $start_of_match + strlen($matches[0]);
if(is_array($lookup_table) && strlen($lookup_table[$matches[1]])>0)
$char = $lookup_table[$matches[1]];
else
$char = chr($matches[1]);
$text = substr($text, 0,$start_of_match).
$char.
substr($text,$end_of_match);
}
return $text;
}
/*
* From php manual
*/
function unhtmlentities($string)
{
$trans_tbl = get_html_translation_table(HTML_ENTITIES);
$trans_tbl = array_flip($trans_tbl);
return strtr($string, $trans_tbl);
}
?>
|