Jump to content

[SOLVED] basic math


Ninjakreborn

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.