menu

Codeigniter String Helper


Codeigniter String Helper – Codeigniter string helper contains functions that will make a random string based on the type and length of arguments. $this->load->helper(‘string’); is used to load the helper. The string helper file contains functions that assist in working with strings . Here in this tutorial, we are going to explain how to use string helper in codeIgniter.


Codeigniter string helper example

Let us first see how to load string helper in codeigniter and then use its function-

Load string helper

How to load string helper in codeingniter example:

Functions:-

There are many functions available in string helper. Now we will explain one by one with example.

    • 1. random string
    • 2. incremented string
    • 3. Alternated string
    • 4. Repeated string
    • 5. normalized slashes string
    • 6. stripped slashes string
    • 7. Slash-trimmed string
    • 8. Reduced string
    • 9. quotes converted string
    • 10. quotes stripped string

    1. Random string

    Syntax of random string function is

    Syntax of random string function is:-

      Parameters:

    • $type (string) : Randomization type
    • $len (int) : Output string length
    • Returns : A random string
    • Return type : string

    This function generates a random string based on the type and length you specify.

    EXAMPLE

    Here is simple example of random string.

    Random string in codeigniter example:-

    The output of the above example will be something like this –

    2. Incremented string

    Syntax of incremented string function is

    Syntax of incremented string function is:-

      Parameters:

    • $str (string) : Input string
    • $separator (string) : Separator to append a duplicate number with
    • $first (int) : Starting number
    • Return : An incremented string
    • Return type : string

    EXAMPLE

    Here is simple example of incremented string.

    Incremented string in codeigniter example:-

    If you run the above example then it will produce output something like this –

    3. Alternated string

    Syntax of alternated string function is

    Syntax of alternated string function is:-

      Parameters:

    • $args (mixed) : A variable number of arguments
    • Return : Alternated string(s)
    • Return type : mixed

    EXAMPLE

    Here is simple example of alternated string.

    Alternated string in codeigniter example:-

    The output of the above example will be something like this –

    4. Repeated string

    Syntax of repeated string function is

    Syntax of repeated string function is:-

      Parameters:

    • $data (string) : Input
    • $num (int) : Number of times to repeat
    • Return : Repeated string
    • Return type : string

    EXAMPLE

    Here is simple example of repeated string.

    Repeated string in codeigniter example:-

    The output of the above example will be something like this –

    5. Normalized slashes string

    Syntax of normalized slashes string function is

    Syntax of normalized slashes string function is:-

      Parameters:

    • $str (string) : Input string
    • $num (int) : Number of times to repeat
    • Return : A string with normalized slashes
    • Return type : string

    This helper converts double slashes string to single slashes.

    EXAMPLE

    Here is simple example of normalized slashes string.

    Normalized slashes string in codeigniter example:-

    The output of example will be like this –

    6. Stripped slashes string

    Syntax of stripped slashes string function is

    Syntax of stripped slashes string function is:-

      Parameters:

    • $data (mixed) : Input string or an array of strings
    • Return : String(s) with stripped slashes
    • Return type : mixed

    This helper remove all slashes from string.

    EXAMPLE

    Here is simple example of stripped slashes string.

    Stripped slashes string in codeigniter example:-

    The output of example will be like this –

    7. Slash-trimmed string

    Syntax of slash-trimmed string function is

    Syntax of slash-trimmed string function is:-

      Parameters:

    • $str (string) : Input string
    • Return : Slash-trimmed string
    • Return type :string

    This helper delete all leading or trailing slashes from string.

    EXAMPLE

    Here is simple example of slash-trimmed string.

    Slash-trimmed string in codeigniter example:-

    The output of example will be like this –

    8. Reduced string

    Syntax of reduced string function is

    Syntax of reduced string function is:-

      Parameters:

    • $str (string) : Text to search in
    • $character (string) : Character to reduce
    • $trim (bool) : Whether to also trim the specified character
    • Return : Reduced string
    • Return type :string

    This helper delete multiple instance of a particular characters.

    EXAMPLE

    Here is simple example of Reduced string.

    Reduced string in codeigniter example:-

    The output of example will be like this –

    9. Quotes converted string

    Syntax of quotes converted string function is

    Syntax of quotes converted string function is:-

      Parameters:

    • $str (string) : Input string
    • Return : String with quotes converted to HTML entities
    • Return type :string

    This helper converts single and double quotes in a string.

    EXAMPLE

    Here is simple example of quotes converted string.

    Quotes converted string in codeigniter example:-

    The output of example will be like this –

    10. Quotes stripped string

    Syntax of quotes stripped string function is

    Syntax of quotes stripped string function is:-

      Parameters:

    • $str (string) : Input string
    • Return : String with quotes stripped
    • Return type :string

    This helper delete single and double quotes to string.

    EXAMPLE

    Here is simple example of quotes stripped string.

    Quotes stripped string in codeigniter example:-

    The output of example will be like this –

Advertisements