CodeIgniter Text helper
0 2473
Text helper contains functions that help us for working with texts.
Related Topics:
Codeigniter Interview Questions
CodeIgniter File helper
CodeIgniter Email helper
Loading this Helper:
Use the given line in your controller or model file to load Text helper.
$this->load->helper('text');
The following functions are available in this helper:
1 word_limiter(): This function is used to truncate a string to the number of words specified.
Syntax:
word_limiter($str[, $limit = 100[, $end_char = '…']]);
Parameter Description:
- $str (string) – Input string
- $limit (int) – Limit
- $end_char (string) – End character (usually an ellipsis)
The return type of this function is a string and it returns a word-limited string.
Example:
Step 1 Open the application/controllers directory and create a new controller file Word_limiter.php
<?php
class Word_limiter extends CI_Controller {
public function index() {
$this->load->helper('text'); //load text helper
$string = "Welcome to CodingTag . It is a Codeigniter tutorial.";
$string = word_limiter($string, 4);
echo $string;
}
}
?>
Step 2 Open the given URL into the browser to see the result.
http://localhost/ci/index.php/word_limiter
2 character_limiter(): This function is used to truncate a string to the number of characters specified.
Syntax:
character_limiter($str[, $n = 500[, $end_char = '…']]);
Parameter Description:
- $str (string) – Input string
- $n (int) – Number of characters
- $end_char (string) – End character (usually an ellipsis)
The return type of this function is a string and it returns a character-limited string.
Example:
Step 1 Open the application/controllers directory and create a new controller file Character_limiter.php
<?php
class Character_limiter extends CI_Controller {
public function index() {
$this->load->helper('text'); //load text helper
$string = "Welcome to CodingTag . It is a Codeigniter tutorial.";
$string = character_limiter($string, 30);
echo $string;
}
}
?>
Step 2 Open the given URL into the browser to see the result.
http://localhost/ci/index.php/Character_limiter
3 ascii_to_entities(): This function is used to convert ASCII values to character entities because high ASCII and MS Word characters generate problems when they are using on a web page.
Syntax:
ascii_to_entities($str);
Parameter Description:
- $str (string) – Input string
The return type of this function is a string and it returns a string with ASCII values converted to entities.
Example:
Step 1 Open the application/controllers directory and create a new controller file Ascii_to_entities.php
<?php
class Ascii_to_entities extends CI_Controller {
public function index() {
$this->load->helper('text'); //load text helper
$string = "Welcome to CodingTag . It is a Codeigniter tutorial.i will display #";
$string = ascii_to_entities($string);
echo $string;
}
}
?>
Step 2 Open the given URL into the browser to see the result.
http://localhost/ci/index.php/Ascii_to_entities
4 convert_accented_characters(): This function is used to transliterates high ASCII characters to low ASCII equivalents.
Syntax:
convert_accented_characters($str);
Parameter Description:
- $str (string) – Input string
The return type of this function is a string and it returns a string with accented characters converted.
To define the to and from the array for transliteration, this function uses a companion config file application/config/foreign_chars.php.
Example:
Step 1 Open application/controllers directory and create a new controller file Convert_accented_characters.php
<?php
class Convert_accented_characters extends CI_Controller {
public function index() {
$this->load->helper('text'); //load text helper
$string = 'tèst Tést ççÇçandãÃ';
$string = convert_accented_characters($string);
echo $string;
}
}
?>
Step 2 Open the given URL into the browser to see the result.
http://localhost/ci/index.php/Convert_accented_characters
5 word_censor(): This function provides the facility to censor words within a text string.
Syntax:
word_censor($str, $censored[, $replacement = '']);
Parameter Description:
- $str (string) – Input string
- $censored (array) – List of bad words to censor
- $replacement (string) – What to replace bad words with
The return type of this function is a string and it returns a censored string.
Example:
Step 1 Open application/controllers directory and create a new controller file Word_censor.php
<?php
class Word_censor extends CI_Controller {
public function index() {
$this->load->helper('text'); //load text helper
$string = 'remove the shucks from maize or shellfish';
$disallowed = array('darn', 'shucks', 'golly', 'phooey');
$string = word_censor($string, $disallowed, 'Beep!');
echo $string;
}
}
?>
Step 2 Open the given URL into the browser to see the result.
http://localhost/ci/index.php/Word_censor
6 highlight_code(): This function is used to colorizes a string of code (PHP, HTML, etc.) by using PHP native function highlight_string() .
Syntax:
highlight_code($str);
Parameter Description:
- $str (string) – Input string
The return type of this function is a string and it returns a string with code highlighted via HTML.
Color should be specified in the php.ini file.
Example:
Step 1 Open application/controllers directory and create a new controller file Highlight_code.php
<?php
class Highlight_code extends CI_Controller {
public function index() {
$this->load->helper('text'); //load text helper
$string = "Welcome to CodingTag . It is a Codeigniter tutorial.;";
$string = highlight_code($string);
echo $string;
}
}
?>
Step 2 Open the given URL into the browser to see the result.
http://localhost/ci/index.php/Highlight_code
7 highlight_phrase(): This function is used to highlight a phrase within a text string.
Syntax:
highlight_phrase($str, $phrase[, $tag_open = '<mark>'[, $tag_close = '</mark>']]);
Parameter Description:
- $str (string) – Input string
- $phrase (string) – Phrase to highlight
- $tag_open (string) – Opening tag used for the highlight
- $tag_close (string) – Closing tag for the highlight
The return type of this function is a string and it returns a string with a phrase highlighted via HTML.
Example:
Step 1 Open application/controllers directory and create a new controller file Highlight_phrase.php
<?php
class Highlight_phrase extends CI_Controller {
public function index() {
$this->load->helper('text'); //load text helper
$string = "Welcome to CodingTag . It is a Codeigniter tutorial.;";
$string = highlight_phrase($string, "CodingTag", '<span style="color:#990000;">', '</span>');
echo $string;
}
}
?>
Step 2 Open the given URL into the browser to see the result.
http://localhost/ci/index.php/Highlight_phrase
8 word_wrap(): This function is used to wrap text at the specified character count while maintaining complete words.
Syntax:
word_wrap($str[, $charlim = 76]);
Parameter Description:
- $str (string) – Input string
- $charlim (int) – Character limit
The return type of this function is a string and it returns a Word-wrapped string.
Example:
Step 1 Open the application/controllers directory and create a new controller file Word_wrap.php
<?php
class Word_wrap extends CI_Controller {
public function index() {
$this->load->helper('text'); //load text helper
$string = "Welcome to CodingTag . It is a Codeigniter tutorial.;";
$string = word_wrap($string, 5);
echo $string;
}
}
?>
Step 2 Open the given URL into the browser to see the result.
http://localhost/ci/index.php/Word_wrap
9 ellipsize(): This function is used to strip tags from a string.
Syntax:
ellipsize($str, $max_length[, $position = 1[, $ellipsis = '…']]);
Parameter Description:
- $str (string) – Input string
- $max_length (int) – String length limit
- $position (mixed) – Position to split at (int or float)
- $ellipsis (string) – What to use as the ellipsis character
The return type of this function is a string and it returns an ellipsized string.
Example:
Step 1 Open the application/controllers directory and create a new controller file Ellipsize.php
<?php
class Ellipsize extends CI_Controller {
public function index() {
$this->load->helper('text'); //load text helper
$str = 'this_string_is_entirely_too_long_and_might_break_my_design.jpg';
$str=ellipsize($str, 32, .5);
echo $str;
}
}
?>
Step 2 Open the given URL into the browser to see the result.
http://localhost/ci/index.php/Ellipsize
Share:
Comments
Waiting for your comments