×

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 file such as:

Syntax

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

Functions of String Helper

  1. random_string(): It is used to generate a random string based on the length and the type of the string that you have set in the random_string() parameters. It is also used to create the passwords or random hashes.

Syntax

random_string ( [ $type = ‘alnum’ [, $len = 5 ]);

It has two parameters:

$type: It defines the type of string you want to create such as alnum, alpha, basic, etc.

$len: It defines the character length.

In the above syntax, the type fieldmay have following values:

  1. alpha: It is used to show a string only in lowercase and uppercase letters.
  2. alnum: It shows the strings in alpha-numeric with uppercase and lowercase letters.
  3. basic: It is a simple basic function to show only random numbers.
  4. numeric: It shows all the random string only in numeric.
  5. nozero: As its names refer, it shows all the random numbers without zeros.
  6. md5: As we know, md5 is used to encrypt the random number or string. By default, it takes 32 characters.
  7. sha1: It is also used to show an encrypted string based on sha1(). It also has a fixed length of 40 characters.

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 string_random_function()  // create a string_random_function ()
     {
         echo "<title> Tutorial and Example </title>";
         $this->load->helper('string');  // load string helper
         echo "alpha string :- ".random_string('alpha', 10)."<br>";
         echo "alnum string :- ". random_string('alnum', 8)."<br>";
         echo "basic string :- ". random_string('basic', 10)."<br>";
         echo "nozero string :- ". random_string('nozero', 12)."<br>";
         echo "numeric string :- ".random_string('numeric', 15)."<br>";
         echo "md5 string :- ".random_string('md5')."<br>";  /* it takes by default 32     characters */
         echo "sha1 string :- ".random_string('sha1')."<br>";  /* it takes by default 40 characters */
 }
     }
 ?> 

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

String Helper
  • increment_string(): As its name suggests, the increment_string () function is used to increment a string by defining a number in a string. It creates a copy or duplicate file that shows unique names.

Syntax

increment_string ($str [, $seperator = ‘_’ [, $first = 2 ] ] );

It has three arguments:

$str: It takes an input string to show a unique title.

$seperator: It is used to separate a number with a string.

$first: It defines the starting number of the string from which you want to start.

Example

echo increment_string ( ‘download’, ‘_’ );  // It prints “download_1”
 echo increment_string ( ‘download’, ‘–‘, 7 );  // It prints “download-7”
 echo increment_string (‘download_10’);  // It prints “download_11” 
  • alternator(): This function is used to display all items in an alternate manner until the loop cycle continues.

Syntax

alternator ($args);

$args: In this field, you can define the variables and conditions of the loop cycle.

Example:

 for ($i = 1; $i < 9; $i++)
 {    
      echo alternator (‘Apple’, ‘Banana’, ‘Mango’, ‘Orange’);
 } 
  • repeater(): As its name suggests, repeaters are used to repeat a word or string with multiple time by defining a number.

Syntax

repeater ( $data , $num= 2 );

It has two parameters:

$data: It defines the string that you want to repeat.

$num: It takes a numeric value that indicates how many times to repeat a string.

Example

$var = “Hello World!”;
 echo repeater ($var, 5); // It will repeat the Hello World! 5 times 
  • reduce_double_slashes(): As the name suggests, this function is used to reduce the double slash of a string to a single slashes strings. It only skips the (http://localhost) slashes URL protocol; after that, it converts the double slash to a single slash. 

Syntax

reduce_double_slashes ($str);

$str: It takes an input string to convert double slashes to a single slash.

Example

$data = “https://tutorialandexample//helpers//string_helper.html”;
 echo reduce_double_slashes ($data);   

echo reduce_double_slashes ($data); 

  • strip_slashes(): It is used to remove any type of slashes from an array of strings.

Syntax

strip_slashes ($data);

$data: It is used to pass an input array or a strings in strip_slashes().

Example

$info= “You can create a website\ using CodeIgniter”;
 echo strip_slashes($info);  
  • trim_slashes(): It is used to trim or remove followers or leading slashes from a string.

Syntax

trim_slashes( $str );

$str: It defines an input string.

Example

$str = “/Hello / hi/ welcome/”;
 echo trim_slashes( $str );  // It prints Hello / hi/ welcome 
  • reduce_multiples(): It is used to remove the copy of a particular characters or a  instances that occurred in a string more than once.

Syntax

reduce_mutiples ($str [, $character = ‘ ‘ [, $trim = FALSE ] ] );

It has three parameters:

$str: It defines a string that contains a repeated character such as ‘,’ and ‘’.

$character: It contains a character that you want to reduce from a string.

$trim: If you want to reduce a specified character from a string, you can set a TRUE value and it will remove that character at the beginning and the end of the string.

Example

$str = “John, Maeve, , Eric, , jimmy, Taylor”;
 echo reduce_multiples( $str ); // It prints John, Maeve, Eric, jimmy, Taylor   
  • quotes_to_entities() : It is used to convert a single and double quotes string into its corresponding HTML tag.

Syntax

quotes_to_entities ($str );

$str: It defines an input string.

Example

$str= "Micheals \"Jackson's \" ";
 $data = quotes_to_entities ($str);   
 echo ($data);   // It print Micheals “Jackson’s” 
  1. strip_quotes(): It is used to remove a single and double quotes from a given string.

Syntax

strip_quotes ($str);

$str: It defines an input string to remove the quotes.

Example

$str = "Micheal's \"Jackson's \" love \"dance\" ";
 $data = strip_quotes ($str);   
 echo ($data);   // it prints Micheals Jacksons love dance 

Create a simple program of string Helper

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 strings_function()
     {
         echo "<title> Tutorial and Example </title>";
         $this->load->helper('string');
         echo "<h2> Functions of String Helper</h2>";
 // random_string function()
 echo "<b>random_string() function </b>";
         echo "alpha string :- ".random_string('alpha', 10). "<br>"; 
 // increment function
 echo "<b>increment_string() function </b>";
         echo increment_string('music', '_'). "<br>"; 
 echo increment_string('download', '-', 8). "<br>"; 
 echo increment_string('file_9'). "<br>"; 
 // alternator function
 echo "<b>alternator() function</b>";
 for ($i = 1; $i <11; $i++)
 {       echo "<br>";
    echo alternator('sunday', 'monday', 'tuesday', 'wednesday', 'friday');
 }   
 echo "<br>";
 // repeater function
 echo "<b>repeater() function</b>";
 $str = "I love Programming";
  $var = repeater($str, 5);
 echo $var."<br>";
 // reduce_double_slashes
 echo "<b>reduce_double_slashes() function</b>";
 $data = "http://tutorialandexample.com//codeigniter//application//string.html";
 echo reduce_double_slashes($data)."<br>"; 
 // strip_slashes function
 echo "<b>strip_slashes() function</b>";
 $str = array(
     'hello'  => 'How are you My friend\'?',
     'hi' => 'I am fine dear.'
 );
 $str = strip_slashes($str);
 print_r($str)."<br>";
 echo "<br>";
     // trim_slashes
     echo "<b>trim_slashes() function</b>";
 $string = "/Codeigniter/application/controller/online/";
 echo trim_slashes($string)."<br>"; 
 // reduce_multiples
 echo "<b>reduce_multiples() function</b>";
 $string = "Eric, Sam,, Wiley, Simmy,,, Honey";
 $string = reduce_multiples($string,","); 
  echo $string."<br>";
 $string = ",Eric, Sam, Wiley, Simmy, Honey,";
 $string = reduce_multiples($string, ", ", TRUE); 
     echo $string;
 echo "<br>";
 // strip_quotes function
 echo "<b>strip_quotes() function</b>";
     $str = "Micheal's \"Jackson's \" love \"dance\" ";
     $data = strip_quotes ($str);   
     echo ($data);   
     echo "<br>";
     // quotes_to_entities
     echo "<b>quotes_to_entities() function</b>";
     $str = "\"Maeve\" hook up with\"Otis\"  ";
     $str= quotes_to_entities ($str); 
     echo $str;
     }
 }
 ?> 

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

String Helper

Related Topics

How to remove an index.php file from the Codeigniter URLs?

As we know, a URL in CodeIgniter is designed in such a way that it would be search engine and human friendly, too, rather than using a standard approach to create a...

2 minutes read.

CodeIgniter Smiley Helper

The smiley helper file contains smiley images that are used in the application for showing emotions and comments while chatting with others. Load a Smiley Helper Before using Smiley Helper in the CodeIgniter application, you...

4 minutes read.

CodeIgniter Number helper

The number helper file contains some predefined function that deals with numeric data to display numbers in bytes format. Load a Number Helper You must load a number helper file in the controller to perform...

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

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.

HTML Table Class CodeIgniter

HTML Table Class The CodeIgniter framework provides an HTML Table Class that is used to develop an auto-generated table using a defined array or result sets of the database table. Loading an HTML Table Class...

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

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.

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 Inflector Helper

The inflector helper file contains some predefined function that allows users to change their English words into plural, singular, and camel case, etc. Loading an Inflector Helper Before using an inflector helper function, you must...

3 minutes read.

CodeIgniter Security Helper

A Security helper file contains some predefined functions that are used to protect application from unauthorized access. Loading the Helper The following syntax is used to load the security helper in the CodeIgniter application. Syntax $this-> load->...

4 minutes read.

CodeIgniter Cookie Helper

A cookie is a small set of files sent from the web server to the end-user system. In Helpers, the cookie helper file has some predefined functions that are used to the...

4 minutes read.

CodeIgniter Tutorial

What is CodeIgniter? CodeIgniter is an open-source framework for developing a dynamic web application with the use of PHP. It uses the MVC framework to develop the project, which is much...

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

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