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:
$this->load->helper('String'); |
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
- $type (string) : Randomization type
- $len (int) : Output string length
- Returns : A random string
- Return type : string
- $str (string) : Input string
- $separator (string) : Separator to append a duplicate number with
- $first (int) : Starting number
- Return : An incremented string
- Return type : string
- $args (mixed) : A variable number of arguments
- Return : Alternated string(s)
- Return type : mixed
- $data (string) : Input
- $num (int) : Number of times to repeat
- Return : Repeated string
- Return type : string
- $str (string) : Input string
- $num (int) : Number of times to repeat
- Return : A string with normalized slashes
- Return type : string
- $data (mixed) : Input string or an array of strings
- Return : String(s) with stripped slashes
- Return type : mixed
- $str (string) : Input string
- Return : Slash-trimmed string
- Return type :string
- $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
- $str (string) : Input string
- Return : String with quotes converted to HTML entities
- Return type :string
- $str (string) : Input string
- Return : String with quotes stripped
- Return type :string
1. Random string
Syntax of random string function is
Syntax of random string function is:-
random_string([$type = 'alnum'[$len = 8]]) |
Parameters:
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:-
//Controllers part public function stringCheck() { $this->load->helper('String'); $this->load->view('string_view'); } // Views parts <?php echo random_string('alpha', 16);?><br> <?php echo random_string('alnum', 16);?><br> <?php echo random_string('basic', 16);?><br> <?php echo random_string('numeric', 16);?><br> <?php echo random_string('nozero', 16);?><br> <?php echo random_string('md5', 16);?><br> <?php echo random_string('sha1', 16);?> |
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:-
increment_string($str[$separator = '_'[$first = 1]]) |
Parameters:
EXAMPLE
Here is simple example of incremented string.
Incremented string in codeigniter example:-
// Views parts <?php echo increment_string('TutorialsPlane', '_'); ?><br> <?php echo increment_string('TutorialsPlane', '-', 2); ?><br> <?php echo increment_string('TutorialsPlane_4'); ?> |
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:-
alternator($args) |
Parameters:
EXAMPLE
Here is simple example of alternated string.
Alternated string in codeigniter example:-
// Views parts <?php echo increment_string('TutorialsPlane', '_'); ?><br> <?php echo increment_string('TutorialsPlane', '-', 2); ?><br> <?php echo increment_string('TutorialsPlane_4'); ?> |
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:-
repeater($data[$num = 1]) |
Parameters:
EXAMPLE
Here is simple example of repeated string.
Repeated string in codeigniter example:-
// Views parts <?php $string = "SOlid Coupon"; echo repeater($string , 5); ?> |
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:-
reduce_double_slashes($str) |
Parameters:
This helper converts double slashes string to single slashes.
EXAMPLE
Here is simple example of normalized slashes string.
Normalized slashes string in codeigniter example:-
// Views parts <?php $string = "http://localhost//Codeigniter//index.php//filepath"; echo reduce_double_slashes($string); ?> |
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:-
strip_slashes($data) |
Parameters:
This helper remove all slashes from string.
EXAMPLE
Here is simple example of stripped slashes string.
Stripped slashes string in codeigniter example:-
// Views parts <?php $str = ("Is your name O\'reilly?No, my na\me is O\'connor"); echo strip_slashes($str); ?> |
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:-
trim_slashes($str) |
Parameters:
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:-
// Views parts <?php $string = "/this/is/SolidCoupon/";?> <?php $paragraph ="/welcome /To/ Tutorialsplane/"?> <?php echo trim_slashes($string);?><br> <?php echo trim_slashes($paragraph);?> |
The output of example will be like this –
8. Reduced string
Syntax of reduced string function is
Syntax of reduced string function is:-
reduce_multiples($str[$character = ''[$trim = FALSE]]) |
Parameters:
This helper delete multiple instance of a particular characters.
EXAMPLE
Here is simple example of Reduced string.
Reduced string in codeigniter example:-
// Views parts <?php $string = "Apple,,,,, Orange,, Mango,,,,,, Grapes"; $string = reduce_multiples($string,","); echo $string; ?><br> <?php $string = ",,,Lion,,,,, Tiger,, Elephant,,,,,, Horse,,,,,"; $string = reduce_multiples($string,",",true); echo $string; ?> |
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:-
quotes_to_entities($str) |
Parameters:
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:-
// Views parts <?php $string = "Rohan's \"dinner\""; $string = quotes_to_entities($string); echo $string; ?> |
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:-
strip_quotes($str) |
Parameters:
This helper delete single and double quotes to string.
EXAMPLE
Here is simple example of quotes stripped string.
Quotes stripped string in codeigniter example:-
// Views parts <?php $string = "Welcome'''' \"Home\""; $string = strip_quotes($string); echo $string; ?> |
The output of example will be like this –
Advertisements