×

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

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 Controller

As its name suggest that it is a Controller that controls the web application, and it works as an intermediary between the Models and the View. A controller takes requests from the user...

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

Download Helper Codeigniter

Download Helper Codeigniter: The download helper function is used to download data from the server to your computer. The data can be any format like text, jpg, mp3, mp4, etc. Loading...

2 minutes read.

CodeIgniter Input Class

Input Class The CodeIgniter framework provides an input class that includes various input functions to pre-process global input data for protection; furthermore, it also provides some helper functions to obtain input data. This...

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

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

Retrieve data from Database

Retrieve data from the Database Now we will discuss how we can retrieve data from the database that we have inserted to the employees table of the database in our previous topic “inserted...

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

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

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.

Migration Class CodeIgniter

The CodeIgniter framework provides the migration class that helps the web developers to create and handle databases in a structured and streamlined manner. The migration classes are very useful when more than...

6 minutes read.

CodeIgniter Libraries

The library is an important part of the CodeIgniter framework. It has a large collection of libraries files that are used to increase the performance of an application. By default, all CodeIgniter libraries...

4 minutes read.

CodeIgniter URL Helper

The helper file contains some predefined functions that are used to work with URL, such as generate the current file path, transfer control, redirect, create a link, etc. Load the URL Helper Before using...

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

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.