Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

need help with a script that should replace last x digits of a number with "x", it does all but the last x

Posted on 2008-06-25
8
Medium Priority
?
225 Views
Last Modified: 2011-10-19
Hi

I have the function attached that takes a string (card number) and should turn the last x digits into "x" to obscure the number. It almost works but turns the last x into "x", it should be everything BUT the last x.

Example: 1234 5678 1234 5678
is becoming 1234 5678 12xx xxxx
it should be xxxx xx34 1234 5678
/**
	* returns part of the parameter
	*	
	* @param string $cardNumber
	* @return	string $safeNumber
	* @access	private
	*/
	private function safeNumber($cardNumber) {	
		$safeNumber = "";
		$char = "x";
		$numToHide = 6;		
		$cardNumber = substr($cardNumber, 0, strlen($cardNumber) - $numToHide);		
		for ($i = 0; $i < $numToHide; $i++) {
				$cardNumber .= $char;
		}			
		for ($startPos = 0; $startPos <= strlen($cardNumber); $startPos+=4)	{
			$safeNumber .= substr($cardNumber, $startPos, 4) . " ";
		}
		return $safeNumber;
	}

Open in new window

0
Comment
Question by:Neil Thompson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 6

Accepted Solution

by:
Iced-evil earned 2000 total points
ID: 21865202
modify this :

$cardNumber = substr($cardNumber, 0, strlen($cardNumber) - $numToHide);            
            for ($i = 0; $i < $numToHide; $i++) {
                        $cardNumber .= $char;
            }                  


into


$cardNumber = substr($cardNumber, $numToHide, strlen($cardNumber));            
            for ($i = 0; $i < $numToHide; $i++) {
                        $cardNumber = $char . $cardNumber;
            }                  
0
 
LVL 9

Expert Comment

by:Rob Siklos
ID: 21865223
This should work:
<?
private function safeNumber($cardNumber) {      
        $safeNumber = "";
        $char = "x";
        $numToHide = 6;         
 
        $numDone = 0;
        
        for ($i = 0; $i < $strlen($cardNumber); $i++) {
            $currChar = substr($cardNumber, $i, 1);
            if (($currChar == ' ') || $numDone > $numToHide) {
                $safeNumber .= $currChar;
            }
            else {
                $safeNumber .= $char;
            }
        }
 
        return $safeNumber;
}

Open in new window

0
 
LVL 9

Expert Comment

by:Rob Siklos
ID: 21865226
shoot - ignore the last one - use this:
<?
private function safeNumber($cardNumber) {      
        $safeNumber = "";
        $char = "x";
        $numToHide = 6;         
 
        $numDone = 0;
        
        for ($i = 0; $i < $strlen($cardNumber); $i++) {
            $currChar = substr($cardNumber, $i, 1);
            if (($currChar == ' ') || $numDone > $numToHide) {
                $safeNumber .= $currChar;
            }
            else {
                $safeNumber .= $char;
                $numDone++;
            }
        }
 
        return $safeNumber;
}

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Closing Comment

by:Neil Thompson
ID: 31470542
Thanks all, this worked!
0
 
LVL 7

Expert Comment

by:dineesh
ID: 21865303
HI Can i recommend a simpler way,

i dont know javascript so have sent a sample in excel, you have to try the same logic in js

=REPT(B3,B2)&RIGHT(B1,LEN(B1)-B2)
safecard.xls
0
 
LVL 2

Expert Comment

by:HengTime
ID: 21865357
i would replace this:

  $cardNumber = substr($cardNumber, 0, strlen($cardNumber) - $numToHide);        
                for ($i = 0; $i < $numToHide; $i++) {
                                $cardNumber .= $char;
                }                      
                for ($startPos = 0; $startPos <= strlen($cardNumber); $startPos+=4)     {
                        $safeNumber .= substr($cardNumber, $startPos, 4) . " ";
                }

with this

$cardNumber = substr($cardNumber, $numToHide );  
$safeNumber = str_repeat("x",$numToHide).$cardNumber;
0
 
LVL 3

Author Comment

by:Neil Thompson
ID: 21865421
Many thanks, all useful comments for me to take worward as I progress and look back over the code to clean it.
0
 
LVL 7

Expert Comment

by:dineesh
ID: 21865521
oops sorry,
i missed the fact that it was php code,  i dont know that either...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question