New on LowEndTalk? Please Register and read our Community Rules.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
Pull the first number out of a string
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:
http://mydomain.tld/item/#######
http://mydomain.anothertld/item/#######
http://mydomain.tld/item/#######/record/####
http://mydomain.tld/item/#######/
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?
thanks,
-drmike
Comments
You forgot to mention in what programming language you want this.
Left it as a tag actually.
Coding is php. Guess best would be a regexp.
Found this: http://www.webmasterworld.com/php/3542421.htm but no solution provided.
what about mydomain3.tld/file.lang?id=67
Would you want the 3 or the 67?
How about something like this: http://pastebin.com/zQMLLSha
@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.
thaks
@drmike: with "mydomain.tld/work/1234/record/5678/" it would output "1234"
http://pastebin.com/9637TUmL
This version will match only the first number with 1 digit or 8 or 9 digits.
@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.
If you already have the URL that you can pop into a string and then use regular expression to pull everything but the numbers.
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.
@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
This returns "1234" for me. Let me know if it works for you:
This will give you the first number sequence found in the string (at least 1 digit, no upperlimit):
is numeric does have it's downfalls since it'll evaulate 32e1 as a number and some other things.
@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
@drmike, not sure I understand what you mean. here's the code running:
http://98.142.216.125/drmike.php
Not a biggie but thanks. I'll post the code and see if anyone had improve it.
If you don't have something like http://mydomain.tld/index1.php3?a=213m4T x&id=12345&user=6547/ what @dannix posted should work
Edit: This one will also match the above url (assuming 12345 is what you need):