Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Advertise on
Pull the first number out of a string
New on LowEndTalk? Please Register and read our Community Rules.

Pull the first number out of a string

drmikedrmike Member
edited August 2011 in General

i think I asked this before but....

I need to pull the first number out of a string,, in this case a url. The url could be in many different forms, such as:





and others. Lots of fun. It can be only a single digit, it can be 8 or 9 digits.

Google pulls up a couple of hits but most of them are for getting the first 'x' characters out of a string.

Any ideas?



  • vldvld Member

    You forgot to mention in what programming language you want this.

    Thanked by 1drmike
  • Left it as a tag actually. :)

    Coding is php. Guess best would be a regexp.

    Found this: but no solution provided.

  • what about mydomain3.tld/file.lang?id=67

    Would you want the 3 or the 67?

    Thanked by 1drmike - The oldest post to host VPS provider
  • vldvld Member

    How about something like this:

    Thanked by 1drmike
  • drmikedrmike Member
    edited August 2011

    @dmmcintyre3, the domain would not have a number in it. Had to check on that as about a dozen domains come into play but, no, none of them have numbers in there. But to answer the question, I would need the 67. You can assume that the domain doesn't contain a number.

    @vld, would that take into account if a url had two numbers in there? For example mydomain.tld/work/1234/record/5678/ I just need and can only get back the first set of numbers.


  • vldvld Member

    @drmike: with "mydomain.tld/work/1234/record/5678/" it would output "1234"

    Thanked by 1drmike
    This version will match only the first number with 1 digit or 8 or 9 digits.

    Thanked by 1drmike

    Repositories: Debian: EU US - Ubuntu: Hiawatha & Others PPA ~ Opensource projects: GitHub

    Support my repositories or my projects: Bitcoin 1SteRu8WRPUqUmE1YXZPZ2cQki8CSGK1r

  • @vld, thanks. I'll take a ook at it in the morning.

    @Steve, it could also have 316 digits in the first bit. My point was we don't know what's in there. We just need the first number out of there, whatever it is. Thanks though. :)

  • edited August 2011

    If you already have the URL that you can pop into a string and then use regular expression to pull everything but the numbers.

      $string = "this is your URL";
      $new_string = ereg_replace("[^0-9]", "", $string );
      echo $new_string;

    You could even work on the regex to keep the / so then you would know that each dataset between the / is separate records. Then pop everything between / in to an array.. Then if you only need a few digits of each use LEN to get the total number and take N amount digits from the string.

    But regardless your first step is a little regex .. it's easy as pie.

    Thanked by 1drmike
  • @Brandon_eNetSouth, I've seen that code before. I believe that takes all of the numbers if I'm not mistaken. The problem is that groups of numbers can appear more than once in the url. I just need the first set.

    thanks though

  • LongShotLongShot Member
    edited August 2011

    This returns "1234" for me. Let me know if it works for you:

    $string = 'mydomain.tld/work/1234/record/5678/';  
    $array = explode('/', $string);
    $key = 0;  
    $piece = $array[$key];  
    while ($key < 10)  
        if (is_numeric($piece)){  
        } else {  
        $key = $key + 1;   
        $piece = $array[$key];  
    Thanked by 1drmike
  • dannixdannix Member
    edited August 2011

    This will give you the first number sequence found in the string (at least 1 digit, no upperlimit):

    $string = 'mydomain.tld/work/1234/record/5678/';
    $pattern = '/[0-9]+/';
    if ( preg_match($pattern,$string,$matches) ) {
    else {
       print "Couldn't match any number pattern";
    Thanked by 1drmike
  • blackblack Member
    edited August 2011

    is numeric does have it's downfalls since it'll evaulate 32e1 as a number and some other things.

    function getDigits ($str){
            $str=explode("/", $str);
            for ($i = 0; $i < sizeof($str); $i++){
                    if (is_numeric($str[$i])){
                            return $str[$i];
    echo getDigits("http://mydomain.tld/item/123456");
    echo getDigits("http://mydomain.anothertld/item/654321");
    echo getDigits("http://mydomain.tld/item/452156/record/0000");
    echo getDigits("http://mydomain.tld/item/561321/");
    Thanked by 1drmike
  • drmikedrmike Member
    edited August 2011

    @vld, nothing strips it appears. The whole url gets passed.

    @Brandon_eNetSouth, yours outputs all of the numbers, not just the first set.

    @Steve81, I can't use your example as it's for a specific length.

    @LongShot, yours depends on a certain setup of the url having 10 levels within the url. i can't use that.

    I'm up to @dannix example.

    Still trying.

    edit: Also just to clarify, I'm trying to pass this back via a return as a variable.

  • @dannix example seems to work.

    Thanks everybody :)

  • vldvld Member

    @drmike, not sure I understand what you mean. here's the code running:

    Thanked by 1drmike
  • Not a biggie but thanks. I'll post the code and see if anyone had improve it. :)

  • vedranvedran Member
    edited August 2011

    If you don't have something like http://mydomain.tld/index1.php3?a=213m4T x&amp;id=12345&amp;user=6547/ what @dannix posted should work

    Edit: This one will also match the above url (assuming 12345 is what you need):

    if (preg_match("/[=\/]([0-9]+)([&\/#].*)?$/", trim($url), $matches)) {
      echo $url . ' returns ' . $matches[1];
    else {
      echo 'No match for ' . $url;
Sign In or Register to comment.