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??
Can you pick out the first 1, 2 or 3 digits from a string?
Moderator: General Moderators
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Can you pick out the first 1, 2 or 3 digits from a string?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: Can you pick out the first 1, 2 or 3 digits from a strin
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
Should be able to do it with preg_match(), don't need preg_match_all()
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: Can you pick out the first 1, 2 or 3 digits from a strin
I think the explode way made work, if you can count how many slashes.
How would that work, given my URL example?
How would that work, given my URL example?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: Can you pick out the first 1, 2 or 3 digits from a strin
Ahhh I've got it. Very very clever indeed.
Many thanks - Explode() works best.
Many thanks - Explode() works best.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.