Letter / Number Replace Code

Discussion in 'PHP' started by mob4u1, Oct 1, 2008.

  1. #1
    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!
     
    If someone posts a solution, use the "Best Answer" link in their post to pick it as the best answer.
    mob4u1, Oct 1, 2008 IP
  2. mob4u1

    mob4u1 Guest

    Best Answers:
    0
    #2
    it doesn't need to REPLACE the name just SHOW the types of characters in the username - hope this makes it clearer.
     
    mob4u1, Oct 1, 2008 Set Best Answer IP
  3. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #3
    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.
     
    ads2help, Oct 1, 2008 Set Best Answer IP
  4. mob4u1

    mob4u1 Guest

    Best Answers:
    0
    #4
    Thanks Great... but:

    i'm still running php4... any alternative for str_split as this is for php5
     
    mob4u1, Oct 1, 2008 Set Best Answer IP
  5. mob4u1

    mob4u1 Guest

    Best Answers:
    0
    #5
    Thats Great, But my server uses php4 so is there a way around the str_split function?
     
    mob4u1, Oct 1, 2008 Set Best Answer IP
  6. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #6
    $str = 'D412REN';
    $whats_in_it = preg_replace('#\d#', 'N', preg_replace('#[A-Z]#i', 'L', $str));
    
    echo $whats_in_it;
    Code (markup):
     
    joebert, Oct 1, 2008 Set Best Answer IP
  7. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #7
    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:
     
    ads2help, Oct 1, 2008 Set Best Answer IP
    1 person likes this.
  8. Sillysoft

    Sillysoft Active Member

    Messages:
    177
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #8
    I agree! preg_replace is less code though you hit the regex engine. But this would be easier.
     
  9. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #9
    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...
     
  10. mob4u1

    mob4u1 Guest

    Best Answers:
    0
    #10
    Thanks to all for your help but using ads2help's version as a starter for my project...
     
    mob4u1, Oct 2, 2008 Set Best Answer IP
  11. mob4u1

    mob4u1 Guest

    Best Answers:
    0
    #11
    ahh, now is there any chance of this being adapted so vowels are shown as a 'V'?
     
    mob4u1, Oct 3, 2008 Set Best Answer IP
  12. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #12
    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:
     
    ads2help, Oct 3, 2008 Set Best Answer IP
  13. mob4u1

    mob4u1 Guest

    Best Answers:
    0
    #13
    You're a star... i'd tried that but had it down as $string instead of $string_
     
    mob4u1, Oct 3, 2008 Set Best Answer IP
  14. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #14
    ahh sorry =D
    Good luck.
     
    ads2help, Oct 3, 2008 Set Best Answer IP
  15. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #15
    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.
     
  16. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #16
    Yes faster. I just learn something : string is array indexable
    sorry and thank you
     
    ads2help, Oct 3, 2008 Set Best Answer IP
  17. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #17
    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.
     
  18. Sillysoft

    Sillysoft Active Member

    Messages:
    177
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #18
    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.