Source for file text_functions.php
Documentation is available at text_functions.php
* An open source application development framework for PHP 4.3.2 or newer
* @author ExpressionEngine Dev Team
* @copyright Copyright (c) 2006, EllisLab, Inc.
* @license http://codeigniter.com/user_guide/license.html
* @link http://codeigniter.com
// ------------------------------------------------------------------------
* CodeIgniter Text Helpers
* @author ExpressionEngine Dev Team
* @link http://codeigniter.com/user_guide/helpers/text_helper.html
// ------------------------------------------------------------------------
* Limits a string to X number of words.
* @param string the end character. Usually an ellipsis
function word_limiter($str, $limit =
100, $end_char =
'…')
preg_match('/^\s*+(?:\S++\s*+){1,'.(int)
$limit.
'}/', $str, $matches);
return rtrim($matches[0]).
$end_char;
// ------------------------------------------------------------------------
* Limits the string based on the character count. Preserves complete words
* so the character count may not be exactly as specified.
* @param string the end character. Usually an ellipsis
if (strlen($out.
$val.
' ') >=
$n)
return trim($out).
$end_char;
// ------------------------------------------------------------------------
* Converts High ascii text and MS Word special characters to character entities
for ($i =
0, $s =
strlen($str); $i <
$s; $i++
)
$ordinal =
ord($str[$i]);
$count =
($ordinal <
224) ?
2 :
3;
if (count($temp) ==
$count)
$number =
($count ==
3) ?
(($temp['0'] %
16) *
4096) +
(($temp['1'] %
64) *
64) +
($temp['2'] %
64) :
(($temp['0'] %
32) *
64) +
($temp['1'] %
64);
$out .=
'&#'.
$number.
';';
// ------------------------------------------------------------------------
* Converts character entities back to ASCII
for ($i =
0, $s =
count($matches['0']); $i <
$s; $i++
)
$digits =
$matches['1'][$i];
$out .=
chr(192 +
(($digits -
($digits %
64)) /
64));
$out .=
chr(128 +
($digits %
64));
$out .=
chr(224 +
(($digits -
($digits %
4096)) /
4096));
$out .=
chr(128 +
((($digits %
4096) -
($digits %
64)) /
64));
$out .=
chr(128 +
($digits %
64));
$str =
str_replace(array("&", "<", ">", """, "'", "-"),
array("&","<",">","\"", "'", "-"),
// ------------------------------------------------------------------------
* Word Censoring Function
* Supply a string and an array of disallowed words and any
* matched words will be converted to #### or to the replacement
* @param string the text string
* @param string the array of censoered words
* @param string the optional replacement value
function word_censor($str, $censored, $replacement =
'')
foreach ($censored as $badword)
// ------------------------------------------------------------------------
* @param string the text string
// The highlight string function encodes and highlights
// brackets so we need them to start raw
$str =
str_replace(array('<', '>'), array('<', '>'), $str);
// Replace any existing PHP tags to temporary markers so they don't accidentally
// break the string out of PHP, and thus, thwart the highlighting.
$str =
str_replace(array('<?', '?>', '<%', '%>', '\\', '</script>'),
array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), $str);
// The highlight_string function requires that the text be surrounded
// by PHP tags. Since we don't know if A) the submitted text has PHP tags,
// or B) whether the PHP tags enclose the entire string, we will add our
// own PHP tags around the string along with some markers to make replacement easier later
$str =
'<?php tempstart'.
"\n".
$str.
'tempend ?>';
// All the magic happens here, baby!
// Prior to PHP 5, the highlight function used icky font tags
// so we'll replace them with span tags.
$str =
str_replace(array('<font ', '</font>'), array('<span ', '</span>'), $str);
$str =
preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str);
// Remove our artificially added PHP
$str =
preg_replace("#\<code\>.+?tempstart\<br />(?:\</span\>)?#is", "<code>\n", $str);
$str =
preg_replace("#tempend.+#is", "</span>\n</code>", $str);
// Replace our markers back to PHP tags.
$str =
str_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
array('<?', '?>', '<%', '%>', '\\', '</script>'), $str);
// ------------------------------------------------------------------------
* Highlights a phrase within a text string
* @param string the text string
* @param string the phrase you'd like to highlight
* @param string the openging tag to precede the phrase with
* @param string the closing tag to end the phrase with
function highlight_phrase($str, $phrase, $tag_open =
'<strong>', $tag_close =
'</strong>')
// ------------------------------------------------------------------------
* Wraps text at the specified character. Maintains the integrity of words.
* Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor
* @param string the text string
* @param integer the number of characters to wrap at
// Se the character limit
// Reduce multiple spaces
// If the current word is surrounded by {unwrap} tags we'll
// strip the entire chunk and replace it with a marker.
for ($i =
0; $i <
count($matches['0']); $i++
)
$unwrap[] =
$matches['1'][$i];
$str =
str_replace($matches['1'][$i], "{{unwrapped".
$i.
"}}", $str);
// Use PHP's native function to do the initial wordwrap.
// We set the cut flag to FALSE so that any individual words that are
// too long get left alone. In the next step we'll deal with them.
$str =
wordwrap($str, $charlim, "\n", FALSE);
// Split the string into individual lines of text and cycle through them
foreach (explode("\n", $str) as $line)
// Is the line within the allowed character count?
// If so we'll join it to the output and continue
if (strlen($line) <=
$charlim)
while((strlen($line)) >
$charlim)
// If the over-length word is a URL we won't wrap it
$temp .=
substr($line, 0, $charlim-
1);
$line =
substr($line, $charlim-
1);
// If $temp contains data it means we had to split up an over-length
// word into smaller chunks so we'll add it back to our current line
$output .=
$temp .
"\n" .
$line;
foreach ($unwrap as $key =>
$val)
$output =
str_replace("{{unwrapped".
$key.
"}}", $val, $output);
// Remove the unwrap tags
$output =
str_replace(array('{unwrap}', '{/unwrap}'), '', $output);
* Esta funcion es identica a la funcion PHP wordwrap pero permite hacer.
* El parametro $cCorte es la cadena de corte que se pone al final de cada linea.
* El parametro $nCorte indica si se debe forzar el corte de linea aun cuando
* quede una cadena cortada por la mitad.
function wordwrap_utf8($cCadena, $nLongitud, $cCorte=
'<br />', $nCorte=
1)
Documentation generated on Tue, 22 Nov 2011 13:29:00 -0200 by phpDocumentor 1.4.3