CodeIgniter Text Helper

The text helper contains the various function that is used to operate with text such as define a word limit and characters limit, conversion of word from ASCII to entities, etc.

Loading the text Helper

Before using the text helper in the CodeIgniter application, you must load it in the controller file such as:

Syntax

$this->load->helper(‘text’);

Functions of text helper

  1. word_limiter(): As its name defines, word_limiter() is used to set a word limit of the text. It also used to truncate the number of words in a string.

Syntax

word_limiter( $str [, $limit = 50 [, $end_char = ‘?’ ] ] );

It has three parameters:

$str: It takes an input string for setting a word_limiter() function.

$limit: It takes a numeric number for setting the word limit in a string.

$end_char: You can set a word at the end of the given string.

  • character_limiter():  It is used to set a character limit in a text. It takes a numeric number for truncating a string.

Syntax

character_limiter ( $str [, $n = 100 [, $end_char = ‘hello’;’] ] );

It has three parameters:

$str: It takes an input string for truncating a string.

$limit: It takes a numeric number for setting a character limit in a string.

$end_char: You can set a character at the end of the given string.

  • word_censor(): It is used to censor words within a text and replace the words with defined strings.

Syntax

word_censor ( $str, $censored [, $replacement = ‘string’ ] );

It has three parameters:

$str: It takes an input string for censoring a given text.

$censored: In this field, you can define bad words to censor in a text.

$replacement: As the name defines, it is used to replace the string with bad censor words.

  • highlight_code(): This function is used to highlight the given code in a text.

Syntax

highlight_code ($str);

$str: It takes an input string that can be highlighted.

  • highlight_phrase(): It is used to highlight a particular phrase or string in a given text.

Syntax

highlight_phrase ($str, $phrase [, $tag_open = ’<mark>’ [, $tag_close = ’</mark>’ ] ] );

It has four parameters:

$str: It takes an input string that can be highlighted.

$phrase: It defines a specific phrase or string to highlight.

$tag_open: It uses an opening tag to highlight in a string.

$tag_close: It uses a closing tag to highlight.

  • word_wrap(): It is used to wrap text at a defined character length while maintaining full words.

Syntax

word_wrap ($str [, $charlim = 20 ] );

It has two parameters:

$str: This fields contains a statements or passage to maintain a complete word. 

$charlim: It set a character limit to wrap the text.

  • ellipsize (): It is used to remove tags from a string, break it into a defined length, and use an ellipsis character.

Syntax

ellipsize ($str, $max_length [, $position = 0 [, $ellipsis = ‘$hellip;’ ] ] );

It has four parameters:

$str: It defines an input string to ellipsize.

$max_length: It defines the maximum number of character should be present in ellipsize().

$position:  It takes a numeric number from 0 to 1 to set the ellipsis position from left to right.

Note: 0 means left, 1 shows the right and .5 shows middle position in the ellipsize() function.

$ellipsis: It defines an ellipsis character that can be inserted. By default, $helip; is used in ellipsize() function.

Example

Create a controller file Online.php and save it in application/controller/Online.php. After that, write the following program in your controller file.

Online.php

<?php
 class Online extends CI_Controller
 { 
 public function Text_helper()
 {
     echo "<title> Tutorial and Example </title>";
     $this->load->helper('text');
     // word_limiter() function
     $string = "Success is the sum of several small efforts repeated often day in and day out";
 $string = word_limiter($string, 6,'GOOD THOUGHT');
 echo $string."<br>";
 // character_limiter() function
 $string = "Success is the sum of several small efforts repeated often day in and day out";
 $string = character_limiter($string, 25,'GOOD Lines');
 echo $string."<br>";
 // word_censor() function
 $str = "You can use PHP to create a web page";
 $censor = array('PHP', 'JAVA', 'C#', 'OTHERS');
 $string = word_censor($str, $censor, 'Codeigniter');
 echo $string."<br>";
 // highlight_code() function
 $var = "Nothing has any power over me other <h2> hello programmar</h2> than that which <?php echo 'Hello love birds' ?>;i give it through my conscious thoughts";
 $string = highlight_code($var);
 echo $string."<br>";
 // highlight_phrase() function
  $data = "This fields contains a statements or passage to maintain a complete word";
 echo highlight_phrase($data, "statements or passage to maintain", '<span style="color:red;">', '</span>')."<br>";
 // word_wrap() function
 $string = "when some circumstances occur in your life then you need to take a small break from everything and spend some time alone to excel your strength.
 echo word_wrap($string, 15)."<br>";
 // ellipsize() function
 $str = 'This function is used to remove tags from a string, break it into a defined length';
 echo ellipsize($str, 35, 1)."<br>";
 }
 }
 ?> 

When you execute the above program in localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/online/text_helper function. It shows the output, as shown below.

Text Helper