×

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

Database Configuration CodeIgniter

In every application, there is an important part of a database through which developers can connect an application to store users’ data and perform various functions in the database application such as inserting,...

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

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

Insert data to the database CodeIgniter

After successfully creating a database connection with the CodeIgniter application, we will now understand how we can insert records into the database. To insert a record into a database table, Codeigniter provides...

8 minutes read.

Calendaring Class CodeIgniter

Calendaring Class The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar...

6 minutes read.

CodeIgniter Routes

The Routes are used to handle URL requests on the browser. It matches the URL request to the predefined routes in CodeIgniter. Therefore, a file must be predefined as a routes.php file to...

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.

CodeIgniter Language Class

Language Class The CodeIgniter’s language class contains the various functionality that is used to access the language-specific files and text line for the purposes of internationalization. This class provides you suitability to add or...

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

Form Validation Library Codeigniter

Form Validation Library The Codeigniter provides a form validation library that contains predefined rules to validate the form fields. It also helps in optimizing the form validation code, which reduces the effort and time...

31 minutes read.

CodeIgniter Models

Models are the classes that deals with backend operations. It is used to fetch the data from the database and send it to the Controller. All the database related information provided inside...

5 minutes read.

Update a Record CodeIgniter

Update a record In this topic, we will learn how we can update existing records in database tables. To update a record, CodeIgniter provides an update() function used with the set()...

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

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

The file helper function is used to perform various tasks with files such as accessing files, writing data to files, deleting files, and more. Loading the File Helper Before using the file helper, you must...

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

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.