Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: testing equality for the last digit in a string

  1. #1
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default testing equality for the last digit in a string

    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
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    What's your code? You should be able to get the last digit very easily:
    echo $string[strlen($string)-1];
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    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.
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  4. #4
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    so,
    Code:
    $rev = strrev($barcode);
    $check = $rev[0];
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,161
    Thanks
    44
    Thanked 3,236 Times in 3,197 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    /.*(\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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    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.
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,161
    Thanks
    44
    Thanked 3,236 Times in 3,197 Posts
    Blog Entries
    12

    Default

    I got the re wrong. It should be:

    Code:
    /.*(\d)[^\d]*$/
    Example:

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

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #9
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    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:
    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);
            }
    	
    }
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  10. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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);
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •