I need a peice of code that will take the submitted username and show the letters and numbers in the username: name inputted: D412REN will show as LNNNLLL is this possible - been racking my brains and got nothing but a headache. Thanks in advance!
it doesn't need to REPLACE the name just SHOW the types of characters in the username - hope this makes it clearer.
jus wrote it for you. Its a function. // assume you got a string stored in variable $string $string = 'D412REN'; function differentiate($string) { $string = str_split($string,1); foreach($string as $string_) { if (is_numeric($string_)) { $return .= 'N'; } else { $return .= 'L'; } } return $return; } // output : LNNNLLL print differentiate($string); PHP: Use this function differentiate($string) whenever you want to split the $string.
$str = 'D412REN'; $whats_in_it = preg_replace('#\d#', 'N', preg_replace('#[A-Z]#i', 'L', $str)); echo $whats_in_it; Code (markup):
oh sorry. PHP4 this is the one $string = 'D412REN'; function differentiate($string) { $string = preg_split('//', $string, -1, PREG_SPLIT_NO_EMPTY); foreach($string as $string_) { if (is_numeric($string_)) { $return .= 'N'; } else { $return .= 'L'; } } return $return; } print differentiate($string); PHP:
People have this irrational fear of the regex engine as 'slow' - It's sure as shine going to be faster to let regex which is native compiled C code handle processing a string than it's going to be having an interpreted loop go through the string one character at a time. Single native function vs. interpreted loop, interpreted type checking, interpreted string building and interpreted conditional? Gee, let me think...
I use the script i gave u last time and made some slight changes $string = 'D412REN'; function differentiate($string) { $string = preg_split('//', $string, -1, PREG_SPLIT_NO_EMPTY); foreach($string as $string_) { if (is_numeric($string_)) { $return .= 'N'; } else { $string_ = strtolower($string_); if ($string_ == 'a' || $string_ == 'e' || $string_ == 'i' || $string_ == 'o' || $string_ == 'u') { $return .= 'V'; } else { $return .= 'L'; } } } return $return; } print differentiate($string); PHP:
I'm looking at that function - why bother with the regexp if you aren't going to use a regexp in the first place, especially since a string is indexable just like an array? Likewise, given the number of results being compared, would not a SWITCH run significantly faster than multiple if/and's? function differentiate($inString) { for ($t=0; $t<strlen($inString); $t++) { if (is_numeric($inString[$t])) { $result.='N'; } else { switch (strtolower($inString[$t])) { case 'a': case 'e': case 'i': case 'o': case 'u': $result.='V'; break; default: $result.='L'; } } } return $result; } Code (markup): Better, no? That's why calling it $string_ is kind of shortsighted and sloppy. $char would have been more appropriate - NOT that I would use a variable with the same exact name as a type... Which is why I renamed them in my rewrite - But that's just me being a disciple at the church of Wirth.
NEVER say sorry if you didn't know something. If I was able to teach you a new trick, then I'm, to borrow from Carlin, more than happy. Programming should always be about learning something new - the day there's nothing new to learn is the day the world leaves you behind.
And?? I simply was agreeing with using regex but mention hitting the regex engine because of the very thing you said. Because people have this "fear" of regex engine. I would use regex in this case. But thanks for taking the time to let me know.