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