4 * @file classes/core/String.inc.php
6 * Copyright (c) 2000-2009 John Willinsky
7 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
12 * @brief String manipulation wrapper class.
16 // $Id: String.inc.php,v 1.18 2009/08/11 21:59:55 mj Exp $
19 * Perl-compatibile regular expression (PCRE) constants:
20 * These are defined application-wide for consistency
24 define('PCRE_URL', '(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?(\/.)?');
26 // RFC-2822 email addresses
27 define('PCRE_EMAIL_ADDRESS',
28 '[-a-z0-9!#\$%&\'\*\+\/=\?\^_\`\{\|\}~]' . '+' . // One or more atom characters.
29 '(\.' . '[-a-z0-9!#\$%&\'\*\+\/=\?\^_\`\{\|\}~]' . '+)*'. // Followed by zero or more dot separated sets of one or more atom characters.
30 '@'. // Followed by an "at" character.
31 '(' . '([a-z0-9]([-a-z0-9]*[a-z0-9]+)?)' . '{1,63}\.)+'. // Followed by one or max 63 domain characters (dot separated).
32 '([a-z0-9]([-a-z0-9]*[a-z0-9]+)?)' . '{2,63}' // Must be followed by one set consisting a period of two or max 63 domain characters.
37 * Perform initialization required for the string wrapper library.
40 $clientCharset = strtolower(Config
::getVar('i18n', 'client_charset'));
42 // Check if mbstring is installed (requires PHP >= 4.3.0)
43 if (String::hasMBString()) {
44 // mbstring routines are available
45 define('ENABLE_MBSTRING', true);
47 // Set up required ini settings for mbstring
48 // FIXME Do any other mbstring settings need to be set?
49 mb_internal_encoding($clientCharset);
50 mb_substitute_character('63'); // question mark
53 // Define modifier to be used in regexp_* routines
54 // FIXME Should non-UTF-8 encodings be supported with mbstring?
55 if ($clientCharset == 'utf-8' && String::hasPCREUTF8()) {
56 define('PCRE_UTF8', 'u');
58 define('PCRE_UTF8', '');
63 * Check if server has the mbstring library.
64 * Currently requires PHP >= 4.3.0 (for mb_strtolower, mb_strtoupper,
65 * and mb_substr_count)
68 function hasMBString() {
70 if (isset($hasMBString)) return $hasMBString;
72 // If string overloading is active, it will break many of the
73 // native implementations. mbstring.func_overload must be set
74 // to 0, 1 or 4 in php.ini (string overloading disabled).
75 if (ini_get('mbstring.func_overload') && defined('MB_OVERLOAD_STRING')) {
79 extension_loaded('mbstring') &&
80 function_exists('mb_strlen') &&
81 function_exists('mb_strpos') &&
82 function_exists('mb_strrpos') &&
83 function_exists('mb_substr') &&
84 function_exists('mb_strtolower') &&
85 function_exists('mb_strtoupper') &&
86 function_exists('mb_substr_count') &&
87 function_exists('mb_send_mail')
94 * Check if server supports the PCRE_UTF8 modifier.
97 function hasPCREUTF8() {
98 // The PCRE_UTF8 modifier is only supported on PHP >= 4.1.0 (*nix) or PHP >= 4.2.3 (win32)
99 // Evil check to see if PCRE_UTF8 is supported
100 if (@preg_match
('//u', '')) {
108 // Wrappers for basic string manipulation routines.
109 // See the phputf8 documentation for usage.
113 * @see http://ca.php.net/manual/en/function.strlen.php
115 function strlen($string) {
116 if (defined('ENABLE_MBSTRING')) {
117 require_once 'mbstring/core.php';
119 require_once 'utils/unicode.php';
120 require_once 'native/core.php';
122 return utf8_strlen($string);
126 * @see http://ca.php.net/manual/en/function.strpos.php
128 function strpos($haystack, $needle, $offset = 0) {
129 if (defined('ENABLE_MBSTRING')) {
130 require_once 'mbstring/core.php';
132 require_once 'utils/unicode.php';
133 require_once 'native/core.php';
135 return utf8_strpos($haystack, $needle, $offset);
139 * @see http://ca.php.net/manual/en/function.strrpos.php
141 function strrpos($haystack, $needle) {
142 if (defined('ENABLE_MBSTRING')) {
143 require_once 'mbstring/core.php';
145 require_once 'utils/unicode.php';
146 require_once 'native/core.php';
148 return utf8_strrpos($haystack, $needle, $offset);
152 * @see http://ca.php.net/manual/en/function.substr.php
154 function substr($string, $start, $length = false) {
155 if (defined('ENABLE_MBSTRING')) {
156 require_once 'mbstring/core.php';
158 require_once 'utils/unicode.php';
159 require_once 'native/core.php';
161 return utf8_substr($string, $start, $length);
165 * @see http://ca.php.net/manual/en/function.strtolower.php
167 function strtolower($string) {
168 if (defined('ENABLE_MBSTRING')) {
169 require_once 'mbstring/core.php';
171 require_once 'utils/unicode.php';
172 require_once 'native/core.php';
174 return utf8_strtolower($string);
178 * @see http://ca.php.net/manual/en/function.strtoupper.php
180 function strtoupper($string) {
181 if (defined('ENABLE_MBSTRING')) {
182 require_once 'mbstring/core.php';
184 require_once 'utils/unicode.php';
185 require_once 'native/core.php';
187 return utf8_strtoupper($string);
191 * @see http://ca.php.net/manual/en/function.substr_count.php
193 function substr_count($haystack, $needle) {
194 if (defined('ENABLE_MBSTRING')) {
195 return mb_substr_count($haystack, $needle); // Requires PHP >= 4.3.0
197 return substr_count($haystack, $needle);
202 * @see http://ca.php.net/manual/en/function.encode_mime_header.php
204 function encode_mime_header($string) {
205 if (defined('ENABLE_MBSTRING')) {
206 return mb_encode_mimeheader($string, mb_internal_encoding(), 'B', MAIL_EOL
);
213 * @see http://ca.php.net/manual/en/function.mail.php
215 function mail($to, $subject, $message, $additional_headers = '', $additional_parameters = '') {
216 // Cannot use mb_send_mail as it base64 encodes the whole body of the email,
217 // making it useless for multipart emails
218 if (empty($additional_parameters)) {
219 return mail($to, $subject, $message, $additional_headers);
221 return mail($to, $subject, $message, $additional_headers, $additional_parameters);
226 // Wrappers for PCRE-compatible regular expression routines.
227 // See the php.net documentation for usage.
231 * @see http://ca.php.net/manual/en/function.regexp_quote.php
233 function regexp_quote($string, $delimiter = '/') {
234 return preg_quote($string, $delimiter);
238 * @see http://ca.php.net/manual/en/function.regexp_grep.php
240 function regexp_grep($pattern, $input) {
241 if (PCRE_UTF8
&& !String::utf8_compliant($input)) $input = String::utf8_bad_strip($input);
242 return preg_grep($pattern . PCRE_UTF8
, $input);
246 * @see http://ca.php.net/manual/en/function.regexp_match.php
248 function regexp_match($pattern, $subject) {
249 if (PCRE_UTF8
&& !String::utf8_compliant($subject)) $subject = String::utf8_bad_strip($subject);
250 return preg_match($pattern . PCRE_UTF8
, $subject);
254 * @see http://ca.php.net/manual/en/function.regexp_match_get.php
256 function regexp_match_get($pattern, $subject, &$matches) {
257 // NOTE: This function was created since PHP < 5.x does not support optional reference parameters
258 if (PCRE_UTF8
&& !String::utf8_compliant($subject)) $subject = String::utf8_bad_strip($subject);
259 return preg_match($pattern . PCRE_UTF8
, $subject, $matches);
263 * @see http://ca.php.net/manual/en/function.regexp_match_all.php
265 function regexp_match_all($pattern, $subject, &$matches) {
266 if (PCRE_UTF8
&& !String::utf8_compliant($subject)) $subject = String::utf8_bad_strip($subject);
267 return preg_match_all($pattern . PCRE_UTF8
, $subject, $matches);
271 * @see http://ca.php.net/manual/en/function.regexp_replace.php
273 function regexp_replace($pattern, $replacement, $subject, $limit = -1) {
274 if (PCRE_UTF8
&& !String::utf8_compliant($subject)) $subject = String::utf8_bad_strip($subject);
275 return preg_replace($pattern . PCRE_UTF8
, $replacement, $subject, $limit);
279 * @see http://ca.php.net/manual/en/function.regexp_replace_callback.php
281 function regexp_replace_callback($pattern, $callback, $subject, $limit = -1) {
282 if (PCRE_UTF8
&& !String::utf8_compliant($subject)) $subject = String::utf8_bad_strip($subject);
283 return preg_replace_callback($pattern . PCRE_UTF8
, $callback, $subject, $limit);
287 * @see http://ca.php.net/manual/en/function.regexp_split.php
289 function regexp_split($pattern, $subject, $limit = -1) {
290 if (PCRE_UTF8
&& !String::utf8_compliant($subject)) $subject = String::utf8_bad_strip($subject);
291 return preg_split($pattern . PCRE_UTF8
, $subject, $limit);
295 * @see http://ca.php.net/manual/en/function.mime_content_type.php
297 function mime_content_type($filename) {
298 if (function_exists('mime_content_type')) {
299 return mime_content_type($filename);
300 } elseif (function_exists('finfo_open')) {
301 $localeFiles =& Registry
::get('fileInfo', true, null);
303 $fi = finfo_open(FILEINFO_MIME
, Config
::getVar('finfo', 'mime_database_path'));
306 return strtok(finfo_file($fi, $filename), ' ;');
310 // Fall back on an external "file" tool
311 $f = escapeshellarg($filename);
312 $result = trim(`file
--brief
--mime
$f`
);
313 // Make sure we just return the mime type.
314 if (($i = strpos($result, ';')) !== false) {
315 $result = trim(substr($result, 0, $i));
322 * Strip unsafe HTML from the input text. Covers XSS attacks like scripts,
323 * onclick(...) attributes, javascript: urls, and special characters.
324 * @param $input string input string
327 function stripUnsafeHtml($input) {
328 // Parts of this implementation were taken from Horde:
329 // see http://cvs.horde.org/co.php/framework/MIME/MIME/Viewer/html.php.
331 $allowedHtml = Config
::getVar('security', 'allowed_html');
332 if ($allowedHtml == '') $allowedHtml = '<a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <b> <i> <u> <img> <sup> <sub> <br> <p>';
334 $html = strip_tags($input, $allowedHtml);
336 // Change space entities to space characters
337 $html = preg_replace('/&#(x0*20|0*32);?/i', ' ', $html);
339 // Remove non-printable characters
340 $html = preg_replace('/&#x?0*([9A-D]|1[0-3]);/i', ' ', $html);
341 $html = preg_replace('/&#x?0*[9A-D]([^0-9A-F]|$)/i', ' \\1', $html);
342 $html = preg_replace('/�*(9|1[0-3])([^0-9]|$)/i', ' \\2', $html);
344 // Remove overly long numeric entities
345 $html = preg_replace('/&#x?0*[0-9A-F]{6,};?/i', ' ', $html);
347 /* Get all attribute="javascript:foo()" tags. This is
348 * essentially the regex /(=|url\()("?)[^>]* script:/ but
349 * expanded to catch camouflage with spaces and entities. */
350 $preg = '/((�*61;?|�*3D;?|=)|'
351 . '((u|�*85;?|�*55;?|�*117;?|�*75;?)\s*'
352 . '(r|�*82;?|�*52;?|�*114;?|�*72;?)\s*'
353 . '(l|�*76;?|�*4c;?|�*108;?|�*6c;?)\s*'
355 . '(�*34;?|�*22;?|"|�*39;?|�*27;?|\')?'
357 . '(s|�*83;?|�*53;?|�*115;?|�*73;?)\s*'
358 . '(c|�*67;?|�*43;?|�*99;?|�*63;?)\s*'
359 . '(r|�*82;?|�*52;?|�*114;?|�*72;?)\s*'
360 . '(i|�*73;?|�*49;?|�*105;?|�*69;?)\s*'
361 . '(p|�*80;?|�*50;?|�*112;?|�*70;?)\s*'
362 . '(t|�*84;?|�*54;?|�*116;?|�*74;?)\s*'
363 . '(:|�*58;?|�*3a;?)/i';
364 $html = preg_replace($preg, '\1\8PKPCleaned', $html);
366 /* Get all on<foo>="bar()". NEVER allow these. */
367 $html = preg_replace('/([\s"\']+'
368 . '(o|�*79;?|�*4f;?|�*111;?|�*6f;?)'
369 . '(n|�*78;?|�*4e;?|�*110;?|�*6e;?)'
370 . '\w+)\s*=/i', '\1PKPCleaned=', $html);
373 '|<([^>]*)&{.*}([^>]*)>|',
374 '|<([^>]*)mocha:([^>]*)>|i',
375 '|<([^>]*)binding:([^>]*)>|i'
377 $replace = array('<&{;}\3>', '<\1PKPCleaned:\2>', '<\1PKPCleaned:\2>');
378 $html = preg_replace($pattern, $replace, $html);
384 // Wrappers for UTF-8 validation routines
385 // See the phputf8 documentation for usage.
389 * Detect whether a string contains non-ascii multibyte sequences in the UTF-8 range
390 * @param $input string input string
393 function utf8_is_valid($str) {
394 require_once 'utils/validation.php';
395 return utf8_is_valid($str);
399 * Tests whether a string complies as UTF-8; faster and less strict than utf8_is_valid
400 * see lib/phputf8/utils/validation.php for more details
401 * @param $input string input string
404 function utf8_compliant($str) {
405 require_once 'utils/validation.php';
406 return utf8_compliant($str);
410 * Locates the first bad byte in a UTF-8 string returning it's byte index in the string
411 * @param $input string input string
414 function utf8_bad_find($str) {
415 require_once 'utils/bad.php';
416 return utf8_bad_find($str);
420 * Strips out any bad bytes from a UTF-8 string and returns the rest
421 * @param $input string input string
424 function utf8_bad_strip($str) {
425 require_once 'utils/bad.php';
426 return utf8_bad_strip($str);
430 * Replace bad bytes with an alternative character - ASCII character
431 * @param $str string input string
432 * @param $replace string optional
435 function utf8_bad_replace($str, $replace = '?') {
436 require_once 'utils/bad.php';
437 return utf8_bad_replace($str, $replace);
441 * Replace bad bytes with an alternative character - ASCII character
442 * @param $input string input string
445 function utf8_strip_ascii_ctrl($str) {
446 require_once 'utils/ascii.php';
447 return utf8_strip_ascii_ctrl($str);
451 * Normalize a string in an unknown (non-UTF8) encoding into a valid UTF-8 sequence
452 * @param $input string input string
455 function utf8_normalize($str) {
456 import('core.Transcoder');
458 if (String::hasMBString()) {
459 // NB: CP-1252 often segfaults; we've left it out here but it will detect as 'ISO-8859-1'
460 $mb_encoding_order = 'UTF-8, UTF-7, ASCII, ISO-8859-1, EUC-JP, SJIS, eucJP-win, SJIS-win, JIS, ISO-2022-JP';
462 if (checkPhpVersion('4.3.8')) {
463 $detected_encoding = mb_detect_encoding($str, $mb_encoding_order, FALSE);
465 $detected_encoding = mb_detect_encoding($str, $mb_encoding_order);
468 } elseif (function_exists('iconv') && strlen(iconv('CP1252', 'UTF-8', $str)) != strlen(iconv('ISO-8859-1', 'UTF-8', $str))) {
469 // use iconv to detect CP-1252, assuming default ISO-8859-1
470 $detected_encoding = 'CP1252';
472 // assume ISO-8859-1, PHP default
473 $detected_encoding = 'ISO-8859-1';
476 // transcode CP-1252/ISO-8859-1 into HTML entities; this works because CP-1252 is mapped onto ISO-8859-1
477 if ('ISO-8859-1' == $detected_encoding ||
'CP1252' == $detected_encoding) {
478 $trans = new Transcoder('CP1252', 'HTML-ENTITIES');
479 $str = $trans->trans($str);
482 // transcode from detected encoding to to UTF-8
483 $trans = new Transcoder($detected_encoding, 'UTF-8');
484 $str = $trans->trans($str);
490 * Returns the UTF-8 string corresponding to the unicode value
491 * Does not require any multibyte PHP libraries
492 * (from php.net, courtesy - romans@void.lv)
496 function code2utf ($num) {
497 if ($num < 128) return chr($num);
498 if ($num < 2048) return chr(($num >> 6) +
192) . chr(($num & 63) +
128);
499 if ($num < 65536) return chr(($num >> 12) +
224) . chr((($num >> 6) & 63) +
128) . chr(($num & 63) +
128);
500 if ($num < 2097152) return chr(($num >> 18) +
240) . chr((($num >> 12) & 63) +
128) . chr((($num >> 6) & 63) +
128) . chr(($num & 63) +
128);
505 * Convert UTF-8 encoded characters in a string to escaped HTML entities
506 * This is a helper function for transcoding into HTML or XML for output
507 * @param $input string input string
510 function utf2html ($str) {
513 $last = 0; // keeps the index of the last regular character
515 for ($i=0; $i<$max; $i++
) {
518 if ($c1>>5 == 6) { // 110x xxxx, 110 prefix for 2 bytes unicode
519 $ret .= substr($str, $last, $i-$last); // append all the regular characters we've passed
520 $c1 &= 31; // remove the 3 bit two bytes prefix
521 $c2 = ord($str{++
$i}); // the next byte
522 $c2 &= 63; // remove the 2 bit trailing byte prefix
523 $c2 |
= (($c1 & 3) << 6); // last 2 bits of c1 become first 2 of c2
524 $c1 >>= 2; // c1 shifts 2 to the right
525 $ret .= "&#" . ($c1 * 0x100 +
$c2) . ";"; // this is the fastest string concatenation
528 elseif ($c1>>4 == 14) { // 1110 xxxx, 110 prefix for 3 bytes unicode
529 $ret .= substr($str, $last, $i-$last); // append all the regular characters we've passed
530 $c2 = ord($str{++
$i}); // the next byte
531 $c3 = ord($str{++
$i}); // the third byte
532 $c1 &= 15; // remove the 4 bit three bytes prefix
533 $c2 &= 63; // remove the 2 bit trailing byte prefix
534 $c3 &= 63; // remove the 2 bit trailing byte prefix
535 $c3 |
= (($c2 & 3) << 6); // last 2 bits of c2 become first 2 of c3
536 $c2 >>=2; //c2 shifts 2 to the right
537 $c2 |
= (($c1 & 15) << 4); // last 4 bits of c1 become first 4 of c2
538 $c1 >>= 4; // c1 shifts 4 to the right
539 $ret .= '&#' . (($c1 * 0x10000) +
($c2 * 0x100) +
$c3) . ';'; // this is the fastest string concatenation
543 $str=$ret . substr($str, $last, $i); // append the last batch of regular characters
549 * Convert numeric HTML entities in a string to UTF-8 encoded characters
550 * This is a native alternative to the buggy html_entity_decode() using UTF8
551 * @param $str string input string
554 function html2utf($str) {
555 // convert named entities to numeric entities
556 $str = strtr($str, String::getHTMLEntities());
558 // use PCRE-aware replace function to replace numeric entities
559 $str = String::regexp_replace('~&#x([0-9a-f]+);~ei', 'String::code2utf(hexdec("\\1"))', $str);
560 $str = String::regexp_replace('~&#([0-9]+);~e', 'String::code2utf(\\1)', $str);
566 * Return an associative array of named->numeric HTML entities
567 * Required to support HTML functions without objects in PHP4/PHP5
568 * From php.net: function.get-html-translation-table.php
571 function getHTMLEntities () {
572 // define the conversion table
573 $html_entities = array(
574 "Á" => "Á", "á" => "á", "Â" => "Â",
575 "â" => "â", "´" => "´", "Æ" => "Æ",
576 "æ" => "æ", "À" => "À", "à" => "à",
577 "ℵ" => "ℵ","Α" => "Α", "α" => "α",
578 "&" => "&", "∧" => "∧", "∠" => "∠",
579 "'" => "'", "Å" => "Å", "å" => "å",
580 "≈" => "≈", "Ã" => "Ã", "ã" => "ã",
581 "Ä" => "Ä", "ä" => "ä", "„" => "„",
582 "Β" => "Β", "β" => "β", "¦" => "¦",
583 "•" => "•", "∩" => "∩", "Ç" => "Ç",
584 "ç" => "ç", "¸" => "¸", "¢" => "¢",
585 "Χ" => "Χ", "χ" => "χ", "ˆ" => "^",
586 "♣" => "♣", "≅" => "≅", "©" => "©",
587 "↵" => "↵", "∪" => "∪", "¤" => "¤",
588 "†" => "†","‡" => "‡", "↓" => "↓",
589 "⇓" => "⇓", "°" => "°", "Δ" => "Δ",
590 "δ" => "δ", "♦" => "♦", "÷" => "÷",
591 "É" => "É", "é" => "é", "Ê" => "Ê",
592 "ê" => "ê", "È" => "È", "è" => "è",
593 "∅" => "∅", " " => " ", " " => " ",
594 "Ε" => "Ε","ε" => "ε","≡" => "≡",
595 "Η" => "Η", "η" => "η", "Ð" => "Ð",
596 "ð" => "ð", "Ë" => "Ë", "ë" => "ë",
597 "€" => "€", "∃" => "∃", "ƒ" => "ƒ",
598 "∀" => "∀","½" => "½", "¼" => "¼",
599 "¾" => "¾", "⁄" => "⁄", "Γ" => "Γ",
600 "γ" => "γ", "≥" => "≥", ">" => ">",
601 "↔" => "↔", "⇔" => "⇔", "♥" => "♥",
602 "…" => "…","Í" => "Í", "í" => "í",
603 "Î" => "Î", "î" => "î", "¡" => "¡",
604 "Ì" => "Ì", "ì" => "ì", "ℑ" => "ℑ",
605 "∞" => "∞", "∫" => "∫", "Ι" => "Ι",
606 "ι" => "ι", "¿" => "¿", "∈" => "∈",
607 "Ï" => "Ï", "ï" => "ï", "Κ" => "Κ",
608 "κ" => "κ", "Λ" => "Λ", "λ" => "λ",
609 "⟨" => "〈", "«" => "«", "←" => "←",
610 "⇐" => "⇐", "⌈" => "⌈",
611 "“" => "“", "≤" => "≤", "⌊" => "⌊",
612 "∗" => "∗","◊" => "◊", "‎" => "‎",
613 "‹" => "‹","‘" => "‘", "<" => "<",
614 "¯" => "¯", "—" => "—", "µ" => "µ",
615 "·" => "·", "−" => "-", "Μ" => "Μ",
616 "μ" => "μ", "∇" => "∇", " " => " ",
617 "–" => "–", "≠" => "≠", "∋" => "∋",
618 "¬" => "¬", "∉" => "∉", "⊄" => "⊄",
619 "Ñ" => "Ñ", "ñ" => "ñ", "Ν" => "Ν",
620 "ν" => "ν", "Ó" => "Ó", "ó" => "ó",
621 "Ô" => "Ô", "ô" => "ô", "Œ" => "Œ",
622 "œ" => "œ", "Ò" => "Ò", "ò" => "ò",
623 "‾" => "‾", "Ω" => "Ω", "ω" => "ω",
624 "Ο" => "Ο","ο" => "ο","⊕" => "⊕",
625 "∨" => "∨", "ª" => "ª", "º" => "º",
626 "Ø" => "Ø", "ø" => "ø", "Õ" => "Õ",
627 "õ" => "õ", "⊗" => "⊗","Ö" => "Ö",
628 "ö" => "ö", "¶" => "¶", "∂" => "∂",
629 "‰" => "‰","⊥" => "⊥", "Φ" => "Φ",
630 "φ" => "φ", "Π" => "Π", "π" => "π",
631 "ϖ" => "ϖ", "±" => "±", "£" => "£",
632 "′" => "′", "″" => "″", "∏" => "∏",
633 "∝" => "∝", "Ψ" => "Ψ", "ψ" => "ψ",
634 """ => """, "√" => "√", "⟩" => "〉",
635 "»" => "»", "→" => "→", "⇒" => "⇒",
636 "⌉" => "⌉", "”" => "”", "ℜ" => "ℜ",
637 "®" => "®", "⌋" => "⌋","Ρ" => "Ρ",
638 "ρ" => "ρ", "‏" => "‏", "›" => "›",
639 "’" => "’", "‚" => "‚", "Š" => "Š",
640 "š" => "š", "⋅" => "⋅", "§" => "§",
641 "­" => "­", "Σ" => "Σ", "σ" => "σ",
642 "ς" => "ς", "∼" => "∼", "♠" => "♠",
643 "⊂" => "⊂", "⊆" => "⊆", "∑" => "∑",
644 "¹" => "¹", "²" => "²", "³" => "³",
645 "⊃" => "⊃", "⊇" => "⊇", "ß" => "ß",
646 "Τ" => "Τ", "τ" => "τ", "∴" => "∴",
647 "Θ" => "Θ", "θ" => "θ", "ϑ" => "ϑ",
648 " " => " ","Þ" => "Þ", "þ" => "þ",
649 "˜" => "~", "×" => "×", "™" => "™",
650 "Ú" => "Ú", "ú" => "ú", "↑" => "↑",
651 "⇑" => "⇑", "Û" => "Û", "û" => "û",
652 "Ù" => "Ù", "ù" => "ù", "¨" => "¨",
653 "ϒ" => "ϒ", "Υ" => "Υ","υ" => "υ",
654 "Ü" => "Ü", "ü" => "ü", "℘" => "℘",
655 "Ξ" => "Ξ", "ξ" => "ξ", "Ý" => "Ý",
656 "ý" => "ý", "¥" => "¥", "ÿ" => "ÿ",
657 "Ÿ" => "Ÿ", "Ζ" => "Ζ", "ζ" => "ζ",
658 "‍" => "‍", "‌" => "‌"
661 return $html_entities;
665 * Wrapper around fputcsv for systems that may or may not support it
666 * (i.e. PHP before 5.1.0); see PHP documentation for fputcsv.
668 function fputcsv(&$handle, $fields = array(), $delimiter = ',', $enclosure = '"') {
669 // From PHP website, thanks to boefje at hotmail dot com
670 if (function_exists('fputcsv')) {
671 return fputcsv($handle, $fields, $delimiter, $enclosure);
675 foreach ($fields as $value) {
676 if ( strpos($value, $delimiter) !== false ||
677 strpos($value, $enclosure) !== false ||
678 strpos($value, "\n") !== false ||
679 strpos($value, "\r") !== false ||
680 strpos($value, "\t") !== false ||
681 strpos($value, ' ') !== false
685 $len = strlen($value);
686 for ($i=0; $i<$len; $i++
) {
687 if ($value[$i] == $escape_char) $escaped = 1;
688 elseif (!$escaped && $value[$i] == $enclosure) $str2 .= $enclosure;
693 $str .= $str2 . $delimiter;
695 $str .= $value . $delimiter;
698 $str = substr($str, 0, -1);
700 return fwrite($handle, $str);
704 * Construct a JSON string to use for AJAX communication
705 * @param $status string The status of an event (e.g. false if form validation fails)
706 * @param $content string The message to be delivered back to the calling script
709 function buildJSON($status = 'true', $content) {
710 return "{'status': $status, 'content': '$content'}";