PageRenderTime 6ms CodeModel.GetById 10ms app.highlight 17ms RepoModel.GetById 23ms app.codeStats 0ms

/system/helpers/text_helper.php

https://github.com/blogaristic/Zebra
PHP | 534 lines | 321 code | 60 blank | 153 comment | 42 complexity | e47aea772e4169212793b4ff5f49f668 MD5 | raw file
  1<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2/**
  3 * CodeIgniter
  4 *
  5 * An open source application development framework for PHP 5.2.4 or newer
  6 *
  7 * NOTICE OF LICENSE
  8 *
  9 * Licensed under the Open Software License version 3.0
 10 *
 11 * This source file is subject to the Open Software License (OSL 3.0) that is
 12 * bundled with this package in the files license.txt / license.rst.  It is
 13 * also available through the world wide web at this URL:
 14 * http://opensource.org/licenses/OSL-3.0
 15 * If you did not receive a copy of the license and are unable to obtain it
 16 * through the world wide web, please send an email to
 17 * licensing@ellislab.com so we can send you a copy immediately.
 18 *
 19 * @package		CodeIgniter
 20 * @author		EllisLab Dev Team
 21 * @copyright	Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
 22 * @license		http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
 23 * @link		http://codeigniter.com
 24 * @since		Version 1.0
 25 * @filesource
 26 */
 27
 28/**
 29 * CodeIgniter Text Helpers
 30 *
 31 * @package		CodeIgniter
 32 * @subpackage	Helpers
 33 * @category	Helpers
 34 * @author		EllisLab Dev Team
 35 * @link		http://codeigniter.com/user_guide/helpers/text_helper.html
 36 */
 37
 38// ------------------------------------------------------------------------
 39
 40if ( ! function_exists('word_limiter'))
 41{
 42	/**
 43	 * Word Limiter
 44	 *
 45	 * Limits a string to X number of words.
 46	 *
 47	 * @param	string
 48	 * @param	int
 49	 * @param	string	the end character. Usually an ellipsis
 50	 * @return	string
 51	 */
 52	function word_limiter($str, $limit = 100, $end_char = '&#8230;')
 53	{
 54		if (trim($str) === '')
 55		{
 56			return $str;
 57		}
 58
 59		preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str, $matches);
 60
 61		if (strlen($str) === strlen($matches[0]))
 62		{
 63			$end_char = '';
 64		}
 65
 66		return rtrim($matches[0]).$end_char;
 67	}
 68}
 69
 70// ------------------------------------------------------------------------
 71
 72if ( ! function_exists('character_limiter'))
 73{
 74	/**
 75	 * Character Limiter
 76	 *
 77	 * Limits the string based on the character count.  Preserves complete words
 78	 * so the character count may not be exactly as specified.
 79	 *
 80	 * @param	string
 81	 * @param	int
 82	 * @param	string	the end character. Usually an ellipsis
 83	 * @return	string
 84	 */
 85	function character_limiter($str, $n = 500, $end_char = '&#8230;')
 86	{
 87		if (strlen($str) < $n)
 88		{
 89			return $str;
 90		}
 91
 92		$str = preg_replace('/\s+/', ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
 93
 94		if (strlen($str) <= $n)
 95		{
 96			return $str;
 97		}
 98
 99		$out = '';
100		foreach (explode(' ', trim($str)) as $val)
101		{
102			$out .= $val.' ';
103
104			if (strlen($out) >= $n)
105			{
106				$out = trim($out);
107				return (strlen($out) === strlen($str)) ? $out : $out.$end_char;
108			}
109		}
110	}
111}
112
113// ------------------------------------------------------------------------
114
115if ( ! function_exists('ascii_to_entities'))
116{
117	/**
118	 * High ASCII to Entities
119	 *
120	 * Converts High ascii text and MS Word special characters to character entities
121	 *
122	 * @param	string
123	 * @return	string
124	 */
125	function ascii_to_entities($str)
126	{
127		$count	= 1;
128		$out	= '';
129		$temp	= array();
130
131		for ($i = 0, $s = strlen($str); $i < $s; $i++)
132		{
133			$ordinal = ord($str[$i]);
134
135			if ($ordinal < 128)
136			{
137				/*
138					If the $temp array has a value but we have moved on, then it seems only
139					fair that we output that entity and restart $temp before continuing. -Paul
140				*/
141				if (count($temp) === 1)
142				{
143					$out  .= '&#'.array_shift($temp).';';
144					$count = 1;
145				}
146
147				$out .= $str[$i];
148			}
149			else
150			{
151				if (count($temp) === 0)
152				{
153					$count = ($ordinal < 224) ? 2 : 3;
154				}
155
156				$temp[] = $ordinal;
157
158				if (count($temp) === $count)
159				{
160					$number = ($count === 3)
161							? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64)
162							: (($temp[0] % 32) * 64) + ($temp[1] % 64);
163
164					$out .= '&#'.$number.';';
165					$count = 1;
166					$temp = array();
167				}
168			}
169		}
170
171		return $out;
172	}
173}
174
175// ------------------------------------------------------------------------
176
177if ( ! function_exists('entities_to_ascii'))
178{
179	/**
180	 * Entities to ASCII
181	 *
182	 * Converts character entities back to ASCII
183	 *
184	 * @param	string
185	 * @param	bool
186	 * @return	string
187	 */
188	function entities_to_ascii($str, $all = TRUE)
189	{
190		if (preg_match_all('/\&#(\d+)\;/', $str, $matches))
191		{
192			for ($i = 0, $s = count($matches[0]); $i < $s; $i++)
193			{
194				$digits = $matches[1][$i];
195				$out = '';
196
197				if ($digits < 128)
198				{
199					$out .= chr($digits);
200
201				}
202				elseif ($digits < 2048)
203				{
204					$out .= chr(192 + (($digits - ($digits % 64)) / 64)).chr(128 + ($digits % 64));
205				}
206				else
207				{
208					$out .= chr(224 + (($digits - ($digits % 4096)) / 4096))
209						.chr(128 + ((($digits % 4096) - ($digits % 64)) / 64))
210						.chr(128 + ($digits % 64));
211				}
212
213				$str = str_replace($matches[0][$i], $out, $str);
214			}
215		}
216
217		if ($all)
218		{
219			return str_replace(array('&amp;', '&lt;', '&gt;', '&quot;', '&apos;', '&#45;'),
220						array('&', '<', '>', '"', "'", '-'),
221						$str);
222		}
223
224		return $str;
225	}
226}
227
228// ------------------------------------------------------------------------
229
230if ( ! function_exists('word_censor'))
231{
232	/**
233	 * Word Censoring Function
234	 *
235	 * Supply a string and an array of disallowed words and any
236	 * matched words will be converted to #### or to the replacement
237	 * word you've submitted.
238	 *
239	 * @param	string	the text string
240	 * @param	string	the array of censoered words
241	 * @param	string	the optional replacement value
242	 * @return	string
243	 */
244	function word_censor($str, $censored, $replacement = '')
245	{
246		if ( ! is_array($censored))
247		{
248			return $str;
249		}
250
251		$str = ' '.$str.' ';
252
253		// \w, \b and a few others do not match on a unicode character
254		// set for performance reasons. As a result words like 端ber
255		// will not match on a word boundary. Instead, we'll assume that
256		// a bad word will be bookeneded by any of these characters.
257		$delim = '[-_\'\"`(){}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]';
258
259		foreach ($censored as $badword)
260		{
261			if ($replacement !== '')
262			{
263				$str = preg_replace("/({$delim})(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")({$delim})/i", "\\1{$replacement}\\3", $str);
264			}
265			else
266			{
267				$str = preg_replace("/({$delim})(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")({$delim})/ie", "'\\1'.str_repeat('#', strlen('\\2')).'\\3'", $str);
268			}
269		}
270
271		return trim($str);
272	}
273}
274
275// ------------------------------------------------------------------------
276
277if ( ! function_exists('highlight_code'))
278{
279	/**
280	 * Code Highlighter
281	 *
282	 * Colorizes code strings
283	 *
284	 * @param	string	the text string
285	 * @return	string
286	 */
287	function highlight_code($str)
288	{
289		/* The highlight string function encodes and highlights
290		 * brackets so we need them to start raw.
291		 *
292		 * Also replace any existing PHP tags to temporary markers
293		 * so they don't accidentally break the string out of PHP,
294		 * and thus, thwart the highlighting.
295		 */
296		$str = str_replace(array('&lt;', '&gt;', '<?', '?>', '<%', '%>', '\\', '</script>'),
297					array('<', '>', 'phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
298					$str);
299
300		// The highlight_string function requires that the text be surrounded
301		// by PHP tags, which we will remove later
302		$str = highlight_string('<?php '.$str.' ?>', TRUE);
303
304		// Remove our artificially added PHP, and the syntax highlighting that came with it
305		$str = preg_replace(array(
306						'/<span style="color: #([A-Z0-9]+)">&lt;\?php(&nbsp;| )/i',
307						'/(<span style="color: #[A-Z0-9]+">.*?)\?&gt;<\/span>\n<\/span>\n<\/code>/is',
308						'/<span style="color: #[A-Z0-9]+"\><\/span>/i'
309					),
310					array(
311						'<span style="color: #$1">',
312						"$1</span>\n</span>\n</code>",
313						''
314					),
315					$str);
316
317		// Replace our markers back to PHP tags.
318		return str_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
319					array('&lt;?', '?&gt;', '&lt;%', '%&gt;', '\\', '&lt;/script&gt;'),
320					$str);
321	}
322}
323
324// ------------------------------------------------------------------------
325
326if ( ! function_exists('highlight_phrase'))
327{
328	/**
329	 * Phrase Highlighter
330	 *
331	 * Highlights a phrase within a text string
332	 *
333	 * @param	string	the text string
334	 * @param	string	the phrase you'd like to highlight
335	 * @param	string	the openging tag to precede the phrase with
336	 * @param	string	the closing tag to end the phrase with
337	 * @return	string
338	 */
339	function highlight_phrase($str, $phrase, $tag_open = '<strong>', $tag_close = '</strong>')
340	{
341		if ($str === '')
342		{
343			return '';
344		}
345
346		if ($phrase !== '')
347		{
348			return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open.'\\1'.$tag_close, $str);
349		}
350
351		return $str;
352	}
353}
354
355// ------------------------------------------------------------------------
356
357if ( ! function_exists('convert_accented_characters'))
358{
359	/**
360	 * Convert Accented Foreign Characters to ASCII
361	 *
362	 * @param	string	the text string
363	 * @return	string
364	 */
365	function convert_accented_characters($str)
366	{
367		global $foreign_characters;
368
369		if ( ! isset($foreign_characters) OR ! is_array($foreign_characters))
370		{
371			if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php'))
372			{
373				include(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php');
374			}
375			elseif (is_file(APPPATH.'config/foreign_chars.php'))
376			{
377				include(APPPATH.'config/foreign_chars.php');
378			}
379
380			if ( ! isset($foreign_characters) OR ! is_array($foreign_characters))
381			{
382				return $str;
383			}
384		}
385
386		return preg_replace(array_keys($foreign_characters), array_values($foreign_characters), $str);
387	}
388}
389
390// ------------------------------------------------------------------------
391
392/**
393 * Word Wrap
394 *
395 * Wraps text at the specified character. Maintains the integrity of words.
396 * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor
397 * will URLs.
398 *
399 * @param	string	the text string
400 * @param	int	the number of characters to wrap at
401 * @return	string
402 */
403if ( ! function_exists('word_wrap'))
404{
405	function word_wrap($str, $charlim = 76)
406	{
407		// Set the character limit
408		if ( ! is_numeric($charlim))
409		{
410			$charlim = 76;
411		}
412
413		// Reduce multiple spaces
414		$str = preg_replace('| +|', ' ', $str);
415
416		// Standardize newlines
417		if (strpos($str, "\r") !== FALSE)
418		{
419			$str = str_replace(array("\r\n", "\r"), "\n", $str);
420		}
421
422		// If the current word is surrounded by {unwrap} tags we'll
423		// strip the entire chunk and replace it with a marker.
424		$unwrap = array();
425		if (preg_match_all('|(\{unwrap\}.+?\{/unwrap\})|s', $str, $matches))
426		{
427			for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
428			{
429				$unwrap[] = $matches[1][$i];
430				$str = str_replace($matches[1][$i], '{{unwrapped'.$i.'}}', $str);
431			}
432		}
433
434		// Use PHP's native function to do the initial wordwrap.
435		// We set the cut flag to FALSE so that any individual words that are
436		// too long get left alone. In the next step we'll deal with them.
437		$str = wordwrap($str, $charlim, "\n", FALSE);
438
439		// Split the string into individual lines of text and cycle through them
440		$output = '';
441		foreach (explode("\n", $str) as $line)
442		{
443			// Is the line within the allowed character count?
444			// If so we'll join it to the output and continue
445			if (strlen($line) <= $charlim)
446			{
447				$output .= $line."\n";
448				continue;
449			}
450
451			$temp = '';
452			while ((strlen($line)) > $charlim)
453			{
454				// If the over-length word is a URL we won't wrap it
455				if (preg_match('!\[url.+\]|://|wwww.!', $line))
456				{
457					break;
458				}
459
460				// Trim the word down
461				$temp .= substr($line, 0, $charlim - 1);
462				$line = substr($line, $charlim - 1);
463			}
464
465			// If $temp contains data it means we had to split up an over-length
466			// word into smaller chunks so we'll add it back to our current line
467			if ($temp !== '')
468			{
469				$output .= $temp."\n".$line."\n";
470			}
471			else
472			{
473				$output .= $line."\n";
474			}
475		}
476
477		// Put our markers back
478		if (count($unwrap) > 0)
479		{
480			foreach ($unwrap as $key => $val)
481			{
482				$output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
483			}
484		}
485
486		// Remove the unwrap tags and return
487		return str_replace(array('{unwrap}', '{/unwrap}'), '', $output);
488	}
489}
490
491// ------------------------------------------------------------------------
492
493if ( ! function_exists('ellipsize'))
494{
495	/**
496	 * Ellipsize String
497	 *
498	 * This function will strip tags from a string, split it at its max_length and ellipsize
499	 *
500	 * @param	string	string to ellipsize
501	 * @param	int	max length of string
502	 * @param	mixed	int (1|0) or float, .5, .2, etc for position to split
503	 * @param	string	ellipsis ; Default '...'
504	 * @return	string	ellipsized string
505	 */
506	function ellipsize($str, $max_length, $position = 1, $ellipsis = '&hellip;')
507	{
508		// Strip tags
509		$str = trim(strip_tags($str));
510
511		// Is the string long enough to ellipsize?
512		if (strlen($str) <= $max_length)
513		{
514			return $str;
515		}
516
517		$beg = substr($str, 0, floor($max_length * $position));
518		$position = ($position > 1) ? 1 : $position;
519
520		if ($position === 1)
521		{
522			$end = substr($str, 0, -($max_length - strlen($beg)));
523		}
524		else
525		{
526			$end = substr($str, -($max_length - strlen($beg)));
527		}
528
529		return $beg.$ellipsis.$end;
530	}
531}
532
533/* End of file text_helper.php */
534/* Location: ./system/helpers/text_helper.php */