Ninjakreborn Posted April 19, 2007 Share Posted April 19, 2007 <?php $variable = "random number here"; $percentage = $variable - 10%; // $percentage allways will contain 90% of whatever the monetary value was in variable ?> Is the above correct, and 100% accurate? Quote Link to comment Share on other sites More sharing options...
Michael Lasky Posted April 19, 2007 Share Posted April 19, 2007 % is the modulus operator in php. You could do something like this instead. $num = 100; echo $num - ($num * 0.1); Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 19, 2007 Share Posted April 19, 2007 No. If you want 90% of something, multiply that something by 0.9 <?php $variable = rand(1,1000); $percentage = $variable * 0.9; ?> Ken Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted April 19, 2007 Author Share Posted April 19, 2007 Ok, not only does this make sense but this will help me heavily in the future. Based on the fact that if I ever need a percentage I can just adjust this. In FACT, I am going to turn this into a usable function right now, to retrieve the percentage of something. Thank you for that, it will be very helpful later. Another question (might as well not create another post for one). I have looked all over the internet for something to explain about how to hide number's in a string. I am accepting bank account numbers, but the client doesn't want to database it, he wants to FIRST strip out everything from it with * and display only the last 4 (this is no matter how many numbers are present, all are * out except the last 4 numbers, THEN databased. I tried looking for something, and I can't find anything. I tried to play around with my own stuff with no luck, string replace. I could use strlen and put down manually like a function $str = strlen($string); if ($string == 5) { $str = "*" . $str; }elseif ($string == 6) { $str = "**" . $str } but in the end it would end up having to be a long function, and there probably is a much easier way that is not so "long". Does anybody have like a function they credit for credit cards or something where you can pass the value, as well as the amount of letter's you want to show. THen it will automatically obscure (with * or something) all the other characters that are there. Quote Link to comment Share on other sites More sharing options...
trq Posted April 19, 2007 Share Posted April 19, 2007 Untested. <?php function hidestring($s,$show=4) { return str_repeat('*',strlen($s)-$show).substr($s,-$show,$show); } echo hidestring('0986456738795'); ?> Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted April 19, 2007 Author Share Posted April 19, 2007 I tried. It doesn't output anything, I tried modifying it, but still no luck. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 19, 2007 Share Posted April 19, 2007 The code that thorpe posted works fine. What does your code look like? Ken Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted April 19, 2007 Author Share Posted April 19, 2007 Ok, I originally tried thorpes, and it didn't work (because it was untested). So I checked each one of the functions to make sure I knew well what they did, and then put this together. <?php // this put's * on all except the ones that are meant to show. // The string si the string you want stripped, show is the total amount you // want showing at the end. function hidestring($string,$show=4) { $length = strlen($string) - $show); $repeat = str_repeat('*', $length); $sub = substr($string, -1, $show); $new = $repeat . $sub; return $new; } ?> Ok, pretty much the same thing just organized where I could understand it all easier. The length, is suppose to be the full length of the variable (whatever that is) minus 4 characters (the 4 that are left, or whatever is passed to show.) The repeat is meant to start repeating the * the right number of times. The sub gets the last 4 characters that were left. Then new takes the repeated * and put's the sub (what's left) at the end. This is outputting nothing though. <?php $accountnumber = mysql_real_escape_string($_POST['acctnum']); echo $accountnumber; echo "<br />"; $accountnumber = hidestring($accountnumber); echo $accountnumber; exit(); ?> That is where I am testing it out at. Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted April 19, 2007 Author Share Posted April 19, 2007 Right now, without passing anything to $show it throws out 7541125415 ******5 That. Quote Link to comment Share on other sites More sharing options...
obsidian Posted April 19, 2007 Share Posted April 19, 2007 Right now, without passing anything to $show it throws out 7541125415 ******5 That. That's because you didn't follow his coding example: <?php // This line $sub = substr($string, -1, $show); // Should be this: $sub = substr($string, -$show, $show); ?> Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted April 19, 2007 Author Share Posted April 19, 2007 Perfect, it works perfectly (thanks thorpe that is going to be a wonderful function to re-use, thanks again.) On to some other stuff, I am making all the same post here, because I have hada few questions I have been checking into in my spare time, these were some longstanding problems I was having. Another thing I was wondering about for awhile has to do with strtotime. Or more of unix timestamps themselfs. If someone get's a date and puts it into a unix time stamp like $date = date(); $date = strtotime($date); I understand this, it saves the time, (hours, minutes, seconds, day, date, and year). Very simple to understand this part. However what I do not understand is $date = date("Y"); // this only get's the year 2007 $date = strtotime($date); based on what I notice, the string is ONLY going to contain the year, how does it generate the other stuff (the day, the month, the date, the time, the hours minutes and second, and everything else when ALL you provide it in this situation is the Y format which is 2007 based format. I have always wondered this. Even after you get string to time and have your unix timestamp you can still do anything with that, and access any of the information, can someone explain this specific thing to me, thanks. Quote Link to comment Share on other sites More sharing options...
trq Posted April 19, 2007 Share Posted April 19, 2007 From the man. The function expects to be given a string containing a US English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT), relative to the timestamp given in now, or the current time if none is supplied. Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted April 19, 2007 Author Share Posted April 19, 2007 Ok, so if you put the year 2007. THen it's going to parse the rest of it at 1-01-2007 12:00 P.M. That type of situation. Then again, if you supply the date and day, and month then will will default to a specific time of day for that day. Like 04-07-2007 It's going to try to start off at the very start of that day right, so it's starting time would be 12:00 A.M. on that specific day. Is this correct, because based on what you put, and what kind of results I am getting by "playing" with it, that's what it seems like it's doing. Quote Link to comment Share on other sites More sharing options...
trq Posted April 19, 2007 Share Posted April 19, 2007 Is this correct Yes. Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted April 19, 2007 Author Share Posted April 19, 2007 Hmm, now I understand the way date's and unix timestamps work a lot better. Thanks for the advice. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.