-1

How to extract only numeric value from "xyz123" string then add 5 in first numeric i.e. 1 and then print xyz623 in final output.

if(isset($login)){
    $length = strlen($pass);
    $c = false;
    for($i=0; $i<$length; $i++){
        if(preg_match('/^[0-9]$/',$pass[$i]) && $c==false){
            $a = (int)$pass[$i];
            $b = $a + 5;
            echo "<br>".$b."<br>";
            $c = true;
        }
    }

}
j08691
  • 204,283
  • 31
  • 260
  • 272
  • 1
    What should happen if the first numeric character is 8? – Niet the Dark Absol Dec 31 '13 at 19:36
  • What would happen if `$b >= 10`? – Grzegorz Dec 31 '13 at 19:37
  • The "password-encryption" tag worries me... – Niet the Dark Absol Dec 31 '13 at 19:38
  • Logic 101: Are all homework problems [toy problems](http://en.wikipedia.org/wiki/Toy_problem)? – bishop Dec 31 '13 at 19:41
  • If character is 8 then value for $b is 13. – VijayPatial Dec 31 '13 at 19:48
  • Before this was edited, it was tagged `password-encryption`. Please, please don't use this as any kind of password encryption scheme. PHP has great [password hashing and management built in.](http://www.php.net/manual/en/faq.passwords.php) – Digital Chris Dec 31 '13 at 19:55
  • To encrypt password using algorithm is my last step but firstly I want to do add some logic to password then I will encrypt. I am beginner but I am trying to add some logic b before using any encryption. – VijayPatial Dec 31 '13 at 20:02
  • @VijayPatial Increment a number inside someone's password doesn't sound logical.. and very very far from encryption. – Mike B Dec 31 '13 at 20:07
  • If we strictly assign password with charcter, special symbol and numeric value then we can add number during registration page and subtract during login. But I don't know is this possible during login if we use decrypt password. – VijayPatial Dec 31 '13 at 20:13
  • @VijayPatial How is that helpful? If I type in xyz123 you're going to mangle that with your 'decryption' before sending it to the database where its stored as zyx623? What are you really trying to accomplish? You don't want to store passwords in a db? – Mike B Dec 31 '13 at 20:16
  • Look @Mike my logic is I have a password field in registration form, if user enter password then we going to check password must contain characters, number and special symbol. secondly we check string with first numeric number in any index of array and we add 5 to that numeric value and we leave next numeric value as it is in a string. third we encrypt the new modified string and fourth stored to database. – VijayPatial Dec 31 '13 at 20:25
  • "secondly we check string with first numeric number in any index of array and we add 5 to that numeric value and we leave next numeric value as it is in a string" - I'll repeat myself, why is that helpful? You're checking minimum requirements for passwords.. good. You're encrypting passwords in the db.. good. Why do you need to find an integer in the middle of the password and add 5?? – Mike B Dec 31 '13 at 20:26
  • To make password more secure isn't it? – VijayPatial Dec 31 '13 at 20:29
  • @VijayPatial You just had 4 different people here tell you that doesn't make the password more secure. – Mike B Dec 31 '13 at 20:30
  • @Mike what you suggest what is good if we want to more secure password? – VijayPatial Dec 31 '13 at 20:37
  • @VijayPatial http://stackoverflow.com/questions/4795385/how-do-you-use-bcrypt-for-hashing-passwords-in-php, http://stackoverflow.com/questions/97984/how-to-secure-database-passwords-in-php – Mike B Dec 31 '13 at 20:38
  • Please explain how xyz623 is any more secure than xyz123. – Niet the Dark Absol Dec 31 '13 at 20:41
  • @NiettheDarkAbsol I tried my best to get an answer to that. – Mike B Dec 31 '13 at 20:42
  • @Mike Encryption and Decryption is good if we add some extra logic in it then what's the deal? – VijayPatial Dec 31 '13 at 20:45
  • @VijayPatial You said "third we encrypt the new modified string and fourth stored to database. " No, no no. Do this: http://www.php.net/manual/en/faq.passwords.php – Digital Chris Dec 31 '13 at 20:45
  • 2
    That's not extra logic, that's BS. – The Blue Dog Dec 31 '13 at 20:45
  • @PeteR if this is BS then why you don't share you knowledge? – VijayPatial Dec 31 '13 at 20:51
  • 1
    Because you'd probably just add 5 to it. – The Blue Dog Dec 31 '13 at 20:52
  • @PeteR What you did during registration page if password need to store in database? – VijayPatial Dec 31 '13 at 20:54
  • @VijayPatial: I use this - http://www.php.net/manual/en/ref.password.php – The Blue Dog Dec 31 '13 at 20:58

1 Answers1

0

Use preg_replace_callback():

$str = 'xyz123';

$output = preg_replace_callback('/([a-z]+)(\d{1})/i', function($m) {
    $added = $m[2] + 5;    // adding 5 to the number
    return $m[1] . $added; // return the concatenated string
}, $str);

echo $output; // => xyz623

Demo.

Amal Murali
  • 75,622
  • 18
  • 128
  • 150