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

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

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

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

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

$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

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

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

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

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

Pin It on Pinterest

Share This