String Helper
The String Helper file contains functions that assist in working with strings.
Loading this Helper
This helper is loaded using the following code:
$this->load->helper('string');
The following functions are available:
random_string()
Generates a random string based on the type and length you specify. Useful for creating passwords or generating random hashes.
The first parameter specifies the type of string, the second parameter specifies the length. The following choices are available:
- alnum: Alpha-numeric string with lower and uppercase characters.
- numeric: Numeric string.
- nozero: Numeric string with no zeros.
- unique: Encrypted with MD5 and uniquid(). Note: The length parameter is not available for this type. Returns a fixed length 33 character string.
Usage example:
echo random_string('alnum', 16);
alternator()
Allows two or more items to be alternated between, when cycling through a loop. Example:
for ($i = 0; $i < 10; $i++)
{
echo alternator('string one', 'string two');
}
You can add as many parameters as you want, and with each iteration of your loop the next item will be returned.
for ($i = 0; $i < 10; $i++)
{
echo alternator('one', 'two', 'three', 'four', 'five');
}
Note: To use multiple separate calls to this function simply call the function with no arguments to re-initialize.
repeater()
Generates repeating copies of the data you submit. Example:
$string = "\n";
echo repeater($string, 30);
The above would generate 30 newlines.
reduce_double_slashes()
onverts double slashes in a string to a single slash, except those found in http://. Example:
$string = "http://www.some-site.com//index.php";
echo reduce_double_slashes($string); // results in "http://www.some-site.com/index.php"
trim_slashes()
Removes any leading/traling slashes from a string. Example:
$string = "/this/that/theother/";
echo trim_slashes($string); // results in this/that/theother