Page 1 of 1

Can you pick out the first 1, 2 or 3 digits from a string?

Posted: Wed Jul 02, 2014 11:01 am
by simonmlewis
We have URLs a bit like this:

http://www.site.co.uk/product/531/Test- ... /126/LARGE

I want to pick out just that THIRD set of digits (the product ID).
I cannot do it by saying "start at character x and pick up the next 3", because it obviously varies.

Can you think of a way I could collect them into a variable??

Re: Can you pick out the first 1, 2 or 3 digits from a strin

Posted: Wed Jul 02, 2014 11:10 am
by Celauran

Re: Can you pick out the first 1, 2 or 3 digits from a strin

Posted: Wed Jul 02, 2014 12:26 pm
by requinix
You can also take the less fun way of explode()ing on slashes and grabbing the 6th (without "http://") or 8th (with) element returned.

Re: Can you pick out the first 1, 2 or 3 digits from a strin

Posted: Wed Jul 02, 2014 5:27 pm
by pickle
Should be able to do it with preg_match(), don't need preg_match_all()

Re: Can you pick out the first 1, 2 or 3 digits from a strin

Posted: Thu Jul 03, 2014 3:12 am
by simonmlewis
I think the explode way made work, if you can count how many slashes.
How would that work, given my URL example?

Re: Can you pick out the first 1, 2 or 3 digits from a strin

Posted: Thu Jul 03, 2014 3:21 am
by simonmlewis
Ahhh I've got it. Very very clever indeed.

Many thanks - Explode() works best.