$v) $val[$k] = self::raw2att($v);
return $val;
} else {
return str_replace(array('&','"',"'",'<','>'),array('&','"',''','<','>'),$val);
}
}
/**
* @see http://www.w3.org/TR/REC-html40/types.html#type-cdata
*/
static function raw2htmlatt($val) {
if(is_array($val)) {
foreach($val as $k => $v) $val[$k] = self::raw2att($v);
return $val;
} else {
$val = str_replace(array('&','"',"'",'<','>'),array('&','"',''','<','>'),$val);
$val = preg_replace('^[a-zA-Z0-9\-_]','_', $val);
$val = preg_replace('^[0-9]*','', $val); //
return $val;
}
}
static function raw2xml($val) {
if(is_array($val)) {
foreach($val as $k => $v) $val[$k] = self::raw2xml($v);
return $val;
} else {
return str_replace(array('&', '<', '>', "\n"), array('&', '<', '>', '
'), $val);
}
}
static function raw2js($val) {
if(is_array($val)) {
foreach($val as $k => $v) $val[$k] = self::raw2js($v);
return $val;
} else {
return str_replace(array("\\", '"',"\n","\r", "'"), array("\\\\", '\"','\n','\r', "\\'"), $val);
}
}
// TODO Possible security risk: doesn't support arrays with more than one level - should be called recursively
static function raw2sql($val) {
if(is_array($val)) {
foreach($val as $k => $v) $val[$k] = self::raw2sql($v);
return $val;
} else {
return addslashes($val);
}
}
/**
* Convert XML to raw text
* @todo Currently xx; entries are stripped; they should be converted
*/
static function xml2raw($val) {
if(is_array($val)) {
foreach($val as $k => $v) $val[$k] = self::xml2raw($v);
return $val;
} else {
// More complex text needs to use html2raw instaed
if(strpos($val,'<') !== false) return self::html2raw($val);
// For simpler stuff, a simple str_replace will do
else {
$converted = str_replace(array('&', '<', '>'), array('&', '<', '>'), $val);
$converted = ereg_replace('[0-9]+;', '', $converted);
return $converted;
}
}
}
static function xml2js($val) {
return self::raw2js(self::html2raw($val));
}
static function xml2att($val) {
return self::raw2att(self::xml2raw($val));
}
static function xml2sql($val) {
return self::raw2sql(self::xml2raw($val));
}
// Convert JS to other formats
static function js2raw($val) {
if(is_array($val)) {
foreach($val as $k => $v) $val[$k] = self::js2raw($v);
return $val;
} else {
return str_replace(array('\"','\n','\r'), array('"',"\n","\r"), $val);
}
}
static function js2xml($val) {
return self::raw2xml(self::js2raw($val));
}
static function js2att($val) {
return self::raw2att(self::js2raw($val));
}
static function js2sql($val) {
return self::raw2sql(self::js2raw($val));
}
static function xml2array($val) {
return preg_split( '/\s*(<[^>]+>)|\s\s*/', $val, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
}
static function array2json( $array ) {
if(function_exists("json_encode")) {
return json_encode($array);
}
$result = array();
// Debug::show($array);
foreach( $array as $key => $value )
if( is_array( $value ) )
$result[] = "'$key':" . Convert::array2json( $value );
else
$result[] = "'$key':'$value'";
return '{' . implode( ', ', $result ) . '}';
}
/**
* Create a link if the string is a valid URL
* @param string The string to linkify
* @return A link to the URL if string is a URL
*/
static function linkIfMatch($string) {
if( preg_match( '/^[a-z+]+\:\/\/[a-zA-Z0-9$-_.+?&=!*\'()%]+$/', $string ) )
return "$string";
else
return $string;
}
/**
* Create a link if the string is a valid URL
* @param string The string to linkify
* @return A link to the URL if string is a URL
*/
/*static function mailtoIfMatch($string) {
if( preg_match( '/^[a-z+]+\:\/\/[a-zA-Z0-9$-_.+?&=!*\'()%]+$/', $string ) )
return "$string";
else
return $string;
}*/
/**
* Simple conversion of HTML to plaintext.
*
* @param $data string
* @param $preserveLinks boolean
* @param $wordwrap array
*/
static function html2raw($data, $preserveLinks = false, $wordWrap = 60, $config = null) {
$defaultConfig = array(
'PreserveLinks' => false,
'ReplaceBoldAsterisk' => true,
'CompressWhitespace' => true,
'ReplaceImagesWithAlt' => true,
);
if(isset($config)) {
$config = array_merge($defaultConfig,$config);
} else {
$config = $defaultConfig;
}
// sTRIp style and script
/* $data = eregi_replace("]*>","", $data);*/
/* $data = eregi_replace("]*>","", $data);*/
$data = preg_replace("/