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

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

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

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

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

  • alternator(): This function is used to display all items in an alternate manner until the loop cycle continues.

Syntax

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

Example:

  • repeater(): As its name suggests, repeaters are used to repeat a word or string with multiple time by defining a number.

Syntax

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

  • 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

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

Example

echo reduce_double_slashes ($data); 

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

Syntax

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

Example

  • trim_slashes(): It is used to trim or remove followers or leading slashes from a string.

Syntax

$str: It defines an input string.

Example

  • 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

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

  • quotes_to_entities() : It is used to convert a single and double quotes string into its corresponding HTML tag.

Syntax

$str: It defines an input string.

Example

  1. strip_quotes(): It is used to remove a single and double quotes from a given string.

Syntax

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

Example

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

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

Pin It on Pinterest

Share This