Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8991867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:49:34+00:00 2026-06-15T22:49:34+00:00

I’m trying to validate a number by it’s length. This number has to have

  • 0

I’m trying to validate a number by it’s length. This number has to have 4 digits so it passes the validation. The problem is when this number has 0’s to it’s left, like 0035.

Right now I’m at this:

echo (strlen ((string) 0025  ));

Which gives a total of 2, but I want this to count the 0’s to it’s left, so it gives me a total of 4.
Clearly the cast of the integer to string is not working, how can i do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:49:35+00:00Added an answer on June 15, 2026 at 10:49 pm

    You can’t do that way, a left zero means the number is octal and not decimal, you can use sprintf() to do that.

    Example:

    echo strlen(sprintf("%04d", 25));
    

    Live Test:
    http://codepad.viper-7.com/VQr7Xz

    Comment Answer:

    I don’t want to add the 0s to the number, i want to detect if the
    number has 0s. If the number received is 25, it’s not a valid number.
    If it is 0025 it is valid. What i want is to validate only numbers
    with 4 digits. – Cláudio Ribeiro

    Cláudio, numbers have infinite left zeros, although a user has explicitly type 2 or 3 left zeros there are more hidden left zeros, it’s a math basic, this is why it’s impossible to know how many left zeros the user has typed if you receive an integer variable. If the variable has a constant size and you want to know how many left zeros it has you can do this:

    <?php
    $int = 25;
     
    echo  4 - strlen($int);
    

    Live test: http://codepad.viper-7.com/fT2jSn

    But if you the variable has variable length it must be a string type instead of a numeric type.

    An example where the variable received is a string:

    <?php
    $strs = array("0025","000035","01","2");
    foreach($strs as $str)
    {
        preg_match("/^0+/", $str, $matches);
        echo strlen(@$matches[0]);
        echo "<br>";
    }
    

    Live Test: http://codepad.viper-7.com/BTRTgR

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.