blob: 712b5750978214e4cd39d80fecf69dac7839a004 [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Derek Jones7f3719f2010-01-05 13:35:37 +00009 * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CodeIgniter Typography Helpers
20 *
21 * @package CodeIgniter
22 * @subpackage Helpers
23 * @category Helpers
24 * @author ExpressionEngine Dev Team
25 * @link http://codeigniter.com/user_guide/helpers/typography_helper.html
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Convert newlines to HTML line breaks except within PRE tags
32 *
33 * @access public
34 * @param string
35 * @return string
36 */
37if ( ! function_exists('nl2br_except_pre'))
38{
39 function nl2br_except_pre($str)
40 {
41 $CI =& get_instance();
42
43 $CI->load->library('typography');
44
45 return $CI->typography->nl2br_except_pre($str);
46 }
47}
48
49// ------------------------------------------------------------------------
50
51/**
52 * Auto Typography Wrapper Function
53 *
54 *
55 * @access public
56 * @param string
Derek Jones2b7de062010-03-05 10:31:08 -060057 * @param bool whether to allow javascript event handlers
Derek Allard2067d1a2008-11-13 22:59:24 +000058 * @param bool whether to reduce multiple instances of double newlines to two
59 * @return string
60 */
61if ( ! function_exists('auto_typography'))
62{
Derek Jones2b7de062010-03-05 10:31:08 -060063 function auto_typography($str, $strip_js_event_handlers = TRUE, $reduce_linebreaks = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000064 {
65 $CI =& get_instance();
66 $CI->load->library('typography');
Derek Jones2b7de062010-03-05 10:31:08 -060067 return $CI->typography->auto_typography($str, $strip_js_event_handlers, $reduce_linebreaks);
68 }
69}
70
71
72// --------------------------------------------------------------------
73
74/**
75 * HTML Entities Decode
76 *
77 * This function is a replacement for html_entity_decode()
78 *
79 * In some versions of PHP the native function does not work
80 * when UTF-8 is the specified character set, so this gives us
81 * a work-around. More info here:
82 * http://bugs.php.net/bug.php?id=25670
83 *
84 * NOTE: html_entity_decode() has a bug in some PHP versions when UTF-8 is the
85 * character set, and the PHP developers said they were not back porting the
86 * fix to versions other than PHP 5.x.
87 *
88 * @access public
89 * @param string
90 * @return string
91 */
92if ( ! function_exists('entity_decode'))
93{
94 function entity_decode($str, $charset='UTF-8')
95 {
96 if (stristr($str, '&') === FALSE) return $str;
97
98 // The reason we are not using html_entity_decode() by itself is because
99 // while it is not technically correct to leave out the semicolon
100 // at the end of an entity most browsers will still interpret the entity
101 // correctly. html_entity_decode() does not convert entities without
102 // semicolons, so we are left with our own little solution here. Bummer.
103
104 if (function_exists('html_entity_decode') && (strtolower($charset) != 'utf-8' OR version_compare(phpversion(), '5.0.0', '>=')))
105 {
106 $str = html_entity_decode($str, ENT_COMPAT, $charset);
107 $str = preg_replace('~&#x(0*[0-9a-f]{2,5})~ei', 'chr(hexdec("\\1"))', $str);
108 return preg_replace('~&#([0-9]{2,4})~e', 'chr(\\1)', $str);
109 }
110
111 // Numeric Entities
112 $str = preg_replace('~&#x(0*[0-9a-f]{2,5});{0,1}~ei', 'chr(hexdec("\\1"))', $str);
113 $str = preg_replace('~&#([0-9]{2,4});{0,1}~e', 'chr(\\1)', $str);
114
115 // Literal Entities - Slightly slow so we do another check
116 if (stristr($str, '&') === FALSE)
117 {
118 $str = strtr($str, array_flip(get_html_translation_table(HTML_ENTITIES)));
119 }
120
121 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 }
123}
124
125/* End of file typography_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000126/* Location: ./system/helpers/typography_helper.php */