×

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

Related Topics

CodeIgniter HTML Helper

The HTML helper function is used in CodeIgniter to provide various functionality, such as headings, images, links, list tags, etc. Loading the HTML Helper Before using the html helper function, you must load the...

7 minutes read.

CodeIgniter Input Class

Input Class The CodeIgniter framework provides an input class that includes various input functions to pre-process global input data for protection; furthermore, it also provides some helper functions to obtain input data. This...

11 minutes read.

Form Validation Library Codeigniter

Form Validation Library The Codeigniter provides a form validation library that contains predefined rules to validate the form fields. It also helps in optimizing the form validation code, which reduces the effort and time...

31 minutes read.

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...

4 minutes read.

CodeIgniter File Helper

The file helper function is used to perform various tasks with files such as accessing files, writing data to files, deleting files, and more. Loading the File Helper Before using the file helper, you must...

7 minutes read.

Migration Class CodeIgniter

The CodeIgniter framework provides the migration class that helps the web developers to create and handle databases in a structured and streamlined manner. The migration classes are very useful when more than...

6 minutes read.

Delete a record from Database CodeIgniter

Delete a record from the Database After inserting, retrieving, and updating table records, we will now learn how we can delete a particular record from a database table. To delete a record, first, we...

6 minutes read.

CodeIgniter User Agent Class

User Agent Class The CodeIgniter provides a user agent class that helps to identify and collect information about the browser, mobile devices, or robots visiting your site. Besides this, it is also used...

4 minutes read.

Calendaring Class CodeIgniter

Calendaring Class The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar...

6 minutes read.

Directory Helper Codeigniter

Directory Helper CodeIgniter: provides a directory helper function that works with the directory. It shows the structure of the file, hidden file in the folder, and the folder in...

3 minutes read.

CodeIgniter String Helper

The string helper file contains functions that allows separate operations with strings in CodeIgniter. Loading the String Helper Before using the string helper in the CodeIgniter application, you must load it in the controller...

7 minutes read.

CodeIgniter Pagination Class

Pagination Class The CodeIgniter framework provides a pagination class that is used in the data list to create pagination links in a web application. Generally, it is used to retrieve data records from...

10 minutes read.

Insert data to the database CodeIgniter

After successfully creating a database connection with the CodeIgniter application, we will now understand how we can insert records into the database. To insert a record into a database table, Codeigniter provides...

8 minutes read.

CodeIgniter CAPTCHA Helper

A CAPTCHA (Complete Automated Public Turning test to tell Computer and Humans Apart) helper is a function that is used in web application to check whether the user is human or machine. It...

3 minutes read.

CodeIgniter Output class

Output class The CodeIgniter framework contains a core class that is an Output class. It  includes the various functions that are used to send the final web page to the requesting browser. In...

8 minutes read.

CodeIgniter Helpers

The Helpers (functions) are stored in the Helper file. It helps CodeIgniter to perform different tasks. The helper file is the collection of many functions that is used to perform specific task. It...

3 minutes read.

CodeIgniter Libraries

The library is an important part of the CodeIgniter framework. It has a large collection of libraries files that are used to increase the performance of an application. By default, all CodeIgniter libraries...

4 minutes read.

Download Helper Codeigniter

Download Helper Codeigniter: The download helper function is used to download data from the server to your computer. The data can be any format like text, jpg, mp3, mp4, etc. Loading...

2 minutes read.

CodeIgniter Email Class

CodeIgniter provides an e-mail library that helps to send a message from one application to another. You can create and send text messages easily in CodeIgniter application even you can set email...

9 minutes read.

Database Configuration CodeIgniter

In every application, there is an important part of a database through which developers can connect an application to store users’ data and perform various functions in the database application such as inserting,...

10 minutes read.