1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Php code to check the end of a string/integer ?

Discussion in 'Programming' started by Jangga, Jan 27, 2017.

  1. Jangga

    Jangga Junior Member

    Joined:
    Aug 8, 2016
    Messages:
    119
    Likes Received:
    4
    Gender:
    Male
    Occupation:
    Freelancer: A reWriter
    Location:
    Africa
    Pls, I'm still stuck on this. I don't know how to check the last two numbers in my string if they end with two zeros. I was using strstr() but it wasn't it. I know jS could have been more appropriate but, I'm not good with jS. So, I'm opting for php

    The variable is

    $price = $_POST["pricetag"];

    The user is to input a value & I want to check if it contains two zeros at the end cos that's what I want and if no two zeros, it should show error. But, how do I set the pointer to check the last two zeros in their price tags?
     
  2. McPatrick

    McPatrick Regular Member

    Joined:
    Feb 1, 2015
    Messages:
    244
    Likes Received:
    103
    Gender:
    Male
    Occupation:
    Problem-solver
    Location:
    London
    Home Page:
  3. Jangga

    Jangga Junior Member

    Joined:
    Aug 8, 2016
    Messages:
    119
    Likes Received:
    4
    Gender:
    Male
    Occupation:
    Freelancer: A reWriter
    Location:
    Africa
    Oh man..… I was hoping to get a quick fix. Seems I'll be reading a lot on the link then.... Thanks
     
  4. Mex

    Mex Jr. VIP Jr. VIP

    Joined:
    Aug 31, 2016
    Messages:
    184
    Likes Received:
    67
    You can use preg_match function to test for regular expressions:
    PHP:
    $string '9.9900';

    $result preg_match('/.+0{2}$/'$string);

    if (
    $result === 1) {
      
        echo 
    "String ends with 00";  

    } else if (
    $result === 0) {

        echo 
    "String doesn't end with 00";  

    }
    This regular expression is a little broad and will match any kind of characters (letters/symbols/numbers) up until the last two characters that need to be two zeroes.
    If the string is formed only by digits you can change it to: '/\d+0{2}$/'

    If you need a more precise regular expression please provide more info about how your string is formed or some examples. :)
     
    • Thanks Thanks x 1
    Last edited: Jan 27, 2017
  5. chillerfx

    chillerfx Jr. VIP Jr. VIP

    Joined:
    Mar 20, 2012
    Messages:
    218
    Likes Received:
    47
    Occupation:
    Web-developer and Digital Marketer.
    Location:
    Everywhere on net.
    Hi,

    Do you want to find and add two decimals after the decimal point if there are no decimals like a price value (25.00).

    echo number_format($num, 2);

    The above mentioned function will add 2 decimals, and avoid more decimal values after the decimal points if the values has more decimals after the decimal points.

    just you would like to check the value has two 0's at the end of the value without any decimals.

    use substr function.
     
    Last edited: Jan 27, 2017
  6. Hambone Oblivion

    Hambone Oblivion Registered Member

    Joined:
    Apr 4, 2016
    Messages:
    64
    Likes Received:
    21
    Location:
    Mitten State
    Thank you very much for this post. This actually helped me figure out an issue I've been having, but haven't taken the time to tackle. Thanks for helping Jangga and myself!
     
    • Thanks Thanks x 1