<?php
class Text {
// NOTE: do to clean what I did below to restore, and rename it...
function cleanStr($orig) {
// if (!get_magic_quotes_gpc())
$slashed = addslashes($orig);
// Debug::debug("Text: before: $orig after: $slashed");
return $slashed;
}
function cleanArray($dirtyArray) {
if ($dirtyArray) {
foreach($dirtyArray as $key => $value)
$cleanArray[$key] = Text::cleanStr($value);
return $cleanArray;
} else
return Array();
}
// right now the only character i think needs replacing is the double-quote;
// and it only needs replacing when added by backend php as a value="value"
// string.
function htmlContentClean($str) {
$problem_chars = array("\"");
$fixed_chars = array(""");
$str = str_replace($problem_chars,$fixed_chars,$str);
return Html::safe_html($str);
}
// WHEW! I hunted this one for a while. long after i introduced
// the policy of taking care of magic quotes et al myself as a
// preparation for handling post and get data, i introduced
// an html form with input type select multiple... this gives
// post not a key-string pair but a key-ARRAY pair, and here
// i was treating all the values from keys as strings! no
// wonder it didn't work... now i handle either situation
// appriopriately, though there is no emergency check for
// infinite recursion (NOTE)
function restore($slashedArrayOrStr) {
if (is_array($slashedArrayOrStr)) {
foreach($slashedArrayOrStr as $key => $value)
$restoreArray[$key] = Text::restore($value);
return $restoreArray;
} else { // ok, it's a string for sure now, so let's clean it!
$restored = stripslashes($slashedArrayOrStr);
//Debug::debug("restoring: before: $value after: $restored");
return $restored;
}
}
function capitalizeFirstChar($str) {
return (strtoupper(substr($str,0,1)) . substr($str,1));
}
function insertSemicolon($str) {
$len = strlen($str);
if (substr($str, $len-1, $len) != ";")
$str .= ";";
return $str;
}
function pluralize($str) {
$len = strlen($str);
if (substr($str, $len-1, $len) != "s")
$str .= "s";
return $str;
}
// finds ideal form field size given a pixel measurement
function numCols($width) {
return (floor($width / 6)-6);
}
function insertStringInKeys($str, $arr) {
foreach($arr as $key => $value)
$new_arr[$str . $key] = $value;
return $new_arr;
}
function smallBreak($num_breaks) {
$break_text = "<font style=\"font-size: 3px\"><br>";
for ($i=0; $i<$num_breaks-1; $i++)
$break_text .= " <br>";
$break_text .= "</font>";
$old_browser_break_text = "<br>" . $break_text;
return Javascript::stringForBrowser($break_text,$old_browser_break_text);
}
function truncate($str, $length=15, $trailing='...') {
if (strlen($str) > $length) {
// string exceeded length, truncate and add trailing dots
return substr($str,0,$length-3).$trailing;
} else {
// string was already short enough, return the string
return $str;
}
}
function removeLastChar($str) {
return (substr($str, 0, strlen($str)-1));
}
function removeLastChars($str, $num_to_remove) {
return (substr($str, 0, strlen($str)-$num_to_remove));
}
// receives a pixel width for the menu box, and returns the number of
// characters to say each menu item has.
function menuCharsWide($width) {
return (floor($width / 5.6)-10);
}
function menuCharsPadding($width, $name) {
$menu_chars_wide = Text::menuCharsWide($width);
return ($menu_chars_wide-strlen($name))*1.4;
}
function aOrAnd($str) {
$first_char = substr($str, 0, 1);
switch($first_char) {
case "a":
case "A":
case "e":
case "E":
case "i":
case "I":
case "o":
case "O":
case "u":
case "U":
return("an $str");
break;
default:
return("a $str");
break;
}
}
}
?>