Posts

Showing posts from September, 2013

Display Unique Number Values in a string using PHP

The funciton DisplayUniqueValues() will let you remove the duplicate values from a string array of numbers and sort out the resulting value that will be displayed. Code: <?php function DisplayUniqueValues($NumArray) { //Remove the duplicated values $uniques = array_unique(explode(",", $NumArray)); //Sort out the unique values $result = sort($uniques, SORT_NUMERIC); //Combine the result in a string using the separator ',' $result = implode(',', $uniques); return $result; } //USAGE $str_num = '12,13,14,12,12,12,12,11,14,90,78,78,91,90,92,89'; echo DisplayUniqueValues($str_num); ?> Output should be: 11,12,13,14,78,89,90,91,92

Password Generator Function in PHP

The following GeneratePassword() function lets you to create a random password whether for your custom site or for personal use. The function contains 1 parameter which is $len . $len - The number of password characters. You can also define the set of characters "CHARS" on your parent class. Code: <?php define("CHARS", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_+@(!)<>"); function GeneratePassword($len = 10) {     $pass = ''; //Initialize random password variable     $char = str_split(CHARS);     $count = strlen(CHARS) - 1;     for ($i = 0; $i < $len; ++$i)     {         $index = rand(1, $count);         $pass .= $char[$index];     }         return $pass; } ?> Usage: <?php       echo GeneratePassword(12); ?>  ...