×

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

Versions of CodeIgniter

CodeIgniter v3.1.11 (This is the current version which we will used in this tutorial)CodeIgniter v3.1.10CodeIgniter v3.1.9CodeIgniter v3.1.8CodeIgniter v3.1.7CodeIgniter v3.1.6CodeIgniter v3.1.5CodeIgniter v3.1.4CodeIgniter v3.1.3CodeIgniter v3.1.2CodeIgniter v3.1.1CodeIgniter v3.1.0CodeIgniter v3.0.6CodeIgniter...

1 minute read.

CodeIgniter Shopping Cart Class

It contains the various function that enables the user to add or update, display, and delete data item from the shopping carts while browsing the site. The items added in the cart...

10 minutes read.

Installation of CodeIgniter

Before installing CodeIgniter, you need to make sure that the wamp or xampp server in the system is preinstalled. The installation procedure for CodeIgniter is very easy. You can download CodeIgniter by following these...

2 minutes read.

Image Manipulation Codeigniter

Image Manipulation A Codeigniter provides an image manipulation class that is used for resizing, cropping, rotation, and many other manipulation tasks. This class also support three major image libraries such as GD/GD2, NetPBM, and...

11 minutes read.

CodeIgniter Encrypt Class

The Codeigniter provides an encrypt class used to protect confidential data stored on computer systems or data transmitted over a network. It provides two-way data encryption techniques such as Mcrypt PHP extension and...

4 minutes read.

CodeIgniter Security Class

Security Class The security class provides various function that helps to create a secure application and process input data securely to the application. This class is preloaded in the CodeIgniter application, so you...

4 minutes read.

CodeIgniter Views

A View is a presentational part which shows the information to the users. It is a complex web page which contains navbar, header, footer, sidebar, etc. A view file cannot be called directly...

5 minutes read.

CodeIgniter Array Helper

The Array Helper contains some predefined functions that are used to perform the various operation with array. Load Array helper It is used to load the helper class. We can pass the helper function in the...

6 minutes read.

CodeIgniter Creating Library

As we know, CodeIgniter has a large collection of library functions that are stored in the system/libraries folder. CodeIgniter also provides some additional functionality in library files. For example, if you want to...

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.

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 Date helper

A date helper file contains some predefined functions that work with date functions. Before using the date function in CodeIgniter, you have to load the date helper in the controller. For example: $this->load->helper(‘date’); Functions of...

12 minutes read.

Structure of Codeigniter

The File structure of CodeIgniter is divided into three parts: ApplicationSystemuser_guide Application -: As the name represents Application, which contains all the main parts of the project like a controller, libraries, view, models, and others....

3 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 Form Helper

A form helper file contains different functions that works with forms. It is used to create various functionality in a form such as dropdown, radio button, password, upload, hidden data, etc. These functionalities...

12 minutes read.

CodeIgniter URI Class

URI Class The CodeIgniter contains a URI class that is used to retrieve information from URI strings. It also provides suitability to get information from the re-routed segments. The system automatically initializes this...

6 minutes read.

CodeIgniter ZIP Encoding Class

ZIP Encoding Class The CodeIgniter provides a Zip encoding class that are used to compress a large file into a Zip archives. And this archives files can be downloaded either to your desktop or...

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

CodeIgniter Models

Models are the classes that deals with backend operations. It is used to fetch the data from the database and send it to the Controller. All the database related information provided inside...

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