Log in

View Full Version : testing equality for the last digit in a string



bernie1227
08-26-2012, 06:18 AM
Hiya guys,
I just thought I might ask a question, just as a change. My question is, I'm testing to see if the last digit of a string is the same as something else (it is meant to ignore the last digit). My problem is however, if there are numbers the same as the last digit, it will ignore them as well, as they are == to the last digit. My problem therefore, is how am I meant to check if is the last digit, rather than equal to the last digit.
thankyasall,
bernie

djr33
08-26-2012, 07:16 AM
What's your code? You should be able to get the last digit very easily:
echo $string[strlen($string)-1];

bernie1227
08-26-2012, 07:20 AM
At the moment, to get the last digit, I am reversing the string and then getting the first digit, ie the last digit normally. The problem however is digits the same as the last digit being counted as the last digit.

bernie1227
08-26-2012, 07:24 AM
so,


$rev = strrev($barcode);
$check = $rev[0];

jscheuer1
08-26-2012, 07:27 AM
I read that few a times and I'm still not sure what you're after. Unless it's also always the last character in the string or always a certain distance from the end or beginning of the string, you can use a regular expression to find the last digit character, otherwise a string function would probably be more efficient.. Once it's isolated you can test it against anything you like or just ignore it altogether. That's the part that was the least clear to me.

A regex for last digit would be:


/.*(\d)D*$/

The parenthetical will match the last digit if any in the string. The preg_match function would probably be your best bet to get that match if any.

bernie1227
08-26-2012, 07:29 AM
thanks, I'll try it, what I am after is something that specifies the actuall last digit, not the number that is the last digit.

jscheuer1
08-26-2012, 08:09 AM
I got the re wrong. It should be:


/.*(\d)[^\d]*$/

Example:


<?php
preg_match('/.*(\d)[^\d]*$/', '1234do9c', $array);
print_r($array);
?>

djr33
08-26-2012, 08:13 AM
I'll just repeat what I had above:


What's your code? You should be able to get the last digit very easily:
echo $string[strlen($string)-1];
That will definitively tell you the end of the string.

If you want to COUNT to the end of the string (eg, how long it is), then you can just use:
strlen($string); (subtract 1 if using this for an array key because that starts with 0)

Beyond that, I have no idea what you're talking about. Again, post some code, or post some example numbers that aren't working for you.



John, what you're suggesting would probably work, but there's no reason to use regex here-- the basic string functions will be faster. It's just counting then selecting a digit. I can't imagine regex being faster for that.

bernie1227
08-26-2012, 08:29 AM
so my code at the moment is checking a UPC barcode (which is found to be valid via some maths, and if the outcome of the maths is equal to the last digit then, its valid), and in the equations you have to ignore the last digit, and at the moment, my code doing that is then ignoring any numbers that are the same as the final number (the check number). Here is a snippet of my code:


$oddNums = array();
$evenNums = array();
$barcode = '123456789999';
$check = $barcode[strlen($barcode)-1];


for($i = 0; $i <= strlen($barcode); $i++){
$focus = $barcode[$i];

if(($i == 0 || ($i % 2) == 0) && $focus != $check){
array_push($oddNums, $focus);
}
if(($i == 1 || (($i % 2) != 0)) && $focus != $check){
array_push($evenNums, $focus);
}

}

djr33
08-26-2012, 08:33 AM
Ok. If I'm following:

Last digit:
echo $string[strlen($string)-1];

Everything UNTIL the last digit:
substr($string,0,-1);

Then do what you want with those.



Also, a slightly prettier (but perhaps not faster) way to do the first (find just the last digit) would be:
substr($string,-1);

bernie1227
08-26-2012, 08:54 AM
Thanks for the reply, the problem is not finding the last digit, but limiting it to the actuall last digit, so my if statements are currently picking up anything the same as the last digit, even if they aren't.

djr33
08-26-2012, 09:08 AM
I'm still barely following.

But I think you can solve this by just only looking at the two substrings-- split the last digit from everything else and then use just those two to compare.

(Also, it looks like you could just limit your for loop by one and never get to the last digit in the loop, but that's just another method.)

bernie1227
08-26-2012, 09:23 AM
I made this modification:


$oddNums = array();
$evenNums = array();
$barcode = '123456789999';
$check = str_split($barcode, (strlen($barcode) - 1));

for($i = 0; $i <= strlen($barcode); $i++){
$focus = $barcode[$i]; //

if(($i == 0 || ($i % 2) == 0) && $focus != $check[1]){
array_push($oddNums, $focus);
}
if(($i == 1 || (($i % 2) != 0)) && $focus != $check[1]){
array_push($evenNums, $focus);
}

}

however the problem is still there, so instead of the oddNums array being:

1, 3, 5, 7, 9, 9,

it's:

1, 3, 5, 7,

as the if statements are ignoring the nine's as they are the same as the final digit, so it thinks that they are the check digit and therefore ignore them.

djr33
08-26-2012, 09:41 AM
I'm really not following what's wrong with that (it's not due to it being identical with the last digit-- try changing them).

Why do you have this part? $focus != $check[1]? I think that's what is getting in the way. Is it supposed to be there?

Here's your code rewritten:

$barcode = '123456789999';
$check = substr($barcode,0,-1);
$last = substr($barcode,-1);

for($i = 0; $i <= strlen($check); $i++){
if($i%2==0){
$oddNums[] = $barcode[$i];
}
else {
$evenNums[] = $barcode[$i];
}
}

//use as you wish:
echo $last; //last digit
echo $check; //all but last digit
print_r($evenNums); //all of the even numbers
print_r($oddNums); //all of the odd numbers

jscheuer1
08-26-2012, 12:50 PM
I was assuming there might be other characters in the string. I did say:


Unless it's also always the last character in the string . . .

People don't always read what you type. At that point bernie1227 should have replied that it is always the last character.
I suppose I could have asked it as a direct question though.

Anyways, thinking that it might not be, I came up with:


<?php
$pattern = '/.*(\d)[^\d]*$/';
$string = '12394do9cas7b21oacpp';
$lastdigit = '';
preg_match($pattern, $string, $array);
print_r($array); //diagnostic
if(isset($array) and isset($array[1])){
$lastdigit = $array[1];
$pos = strrpos($string, $lastdigit);
echo '<br>' . $lastdigit . ' is the last digit in' . $string . ', ' . $pos . ' characters from it\'s beginning<br>';
echo 'Here\'s the string without it: ' . substr($string, 0, $pos) . substr($string, $pos +1);
} else {
echo '<br>Either there is no string, or it contains no digits';
}
?>

The above will also work with all numbers.

If it is always the last character, Daniel's method is more or less what I had in mind, go with it.

jscheuer1
08-27-2012, 03:58 AM
So, you figure this one out yet? In any case, try this:


<?php
$string = '16598236';
echo 'Starting String: ' . $string . '<br>';
$lastindex = strlen($string) - 1;
$lastdigit = $string{$lastindex};
echo 'Last Char: ' . $lastdigit . '<br>';
$shortstring = substr($string, 0, $lastindex);
echo 'Shortened String: ' . $shortstring . '<br>';
?>

bernie1227
08-27-2012, 08:52 AM
thanks for the replies John, Haven't figured it out yet, I'm pretty sure I sad numerous times that I was after the last digit :p. I don't have time at the moment, but could you be so kind as to add this check and test it?


<?php
$string = '16598236';
echo 'Starting String: ' . $string . '<br>';
$lastindex = strlen($string) - 1;
$lastdigit = $string{$lastindex};
echo 'Last Char: ' . $lastdigit . '<br>';
$shortstring = substr($string, 0, $lastindex);
echo 'Shortened String: ' . $shortstring . '<br>';

if($lastdigit == 6){
echo "see what I mean?";
}
else{
echo "wow, it works!";
}
?>

you'll see what I've been trying to say. My main problem is that for the example here, my current code thinks that anything the same as the last digit, ie. 6, must be the last digit.
Do you see what I'm trying to say?
thanks for your continued efforts,
Bernie

jscheuer1
08-27-2012, 09:25 AM
6 will always == 6, there's no getting around that. But I think you've "painted yourself into a corner" if you think you have to make it otherwise. Clearly your objective cannot be to prove that a number isn't equal to itself. What're you looking for? If you wish to process each number in the string except the last number, use (from this most recent code) the $shortstring variable. It contains all the numbers in their original order except the last one.

If you have some other objective, what might that be?

bernie1227
08-27-2012, 09:47 AM
ahhhhhh,
I see what you're saying now, thankyou very much John :)

djr33
08-27-2012, 10:18 AM
Bernie, I'm still completely lost. Read what you wrote earlier and see if it makes sense to you:

I'm pretty sure I sad numerous times that I was after the last digit
We've given you code to find the last digit.

Are you intending some other use of "last"? I'm really confused.... not trying to be difficult.

Or have you now solved it?

If not, maybe you misunderstood the algorithm for a barcode. I don't know what it is, but what you've said hasn't been entirely clear either.

bernie1227
08-27-2012, 10:47 AM
no, I didn't misunderstand it, I was simply misunderstanding what I was trying to do, don't worry, I've solved it now :p