0

I have this dummy URL : http://blahblah.co.uk/node/57?url=/Installer/Manufacturer/17/Schuco

I need the number "17" from this URL in order to query the database.

I have looked into using "isset()" but am not sure it is the right approach.

Can someone show me how to strip this URL down?

Thanks

This is what I have so far:

$url = $_GET['url'];
echo $url;
1
  • Already have. sorry, only just updated the code @Daan Commented Oct 1, 2014 at 10:02

3 Answers 3

1

You could use explode() to strip the $url and get the 17

$url = $_GET['url'];
$parts = Explode('/', $url);
$part = $parts[7]; //17

You can change the 7 to get the right part of the url.

edit (after comment):

print_r($parts);

will give you

Array ( [0] => http: 
        [1] => 
        [2] => blahblah.co.uk 
        [3] => node 
        [4] => 57?url= 
        [5] => Installer 
        [6] => Manufacturer 
        [7] => 17 
        [8] => Schuco 
)

So you can see that number 7 gives you 17
echo $parts[7]; // 17

Sign up to request clarification or add additional context in comments.

5 Comments

So why is the array value 4? @C Travel
Good question, after testing i came to the conclusion it was number 7, when you are exploding the full url
Ahh that makes more sense. Thanks @C Travel
Updates my answers with the output of print_r($parts);
So basically "Explode" just splits strings that are separated by common things like "/" in to array items? @C Travel
1

Better if you will use PHP function parse_url to parse the URL.

Check below code :

$url = 'http://blahblah.co.uk/node/57?url=/Installer/Manufacturer/17/Schuco';
$parsedUrl = parse_url($url);
$explodeUrl = explode('/', $parsedUrl['query']);

echo $explodeUrl[3]; // 17

Now, you will get all the details of the URL you passed.

4 Comments

The number will change though depending on the product they choose. So setting the URL as a variable may not be the best approach. @prava
@sMilbz, can you please give 1-2 examples of such, so that I can come up with a definite solution :)
Well there are 57 products in total, so every time a product is selected, the "17" will correlate to the product ID. The rest of the url will stay the same @prava
@sMilbz, what I got from your comment, the link will be same everytime, only the ID will change, like - blahblah.co.uk/node/57?url=/Installer/Manufacturer/17/Schuco OR blahblah.co.uk/node/57?url=/Installer/Manufacturer/32/Schuco OR blahblah.co.uk/node/57?url=/Installer/Manufacturer/212/Schuco ... If the URL will come like above, then no worries :) the above code will work for all :)
1

Try with preg_match()

$str = 'http://blahblah.co.uk/node/57?url=/Installer/Manufacturer/17/Schuco';
preg_match_all('/[0-9]+/', $str, $matches);
print($matches[0][1]); // 17

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.