How to split a link into "multiple parts"?

Asked

Viewed 254 times

1

I have the following link:

https://steamcommunity.com/tradeoffer/new/?partner=377361903&token=9Q0WYuq0

What I want to do is to store the value of Partner in a variable called $partner, and the token value, stored in a variable called $token.

How can I do that?

2 answers

1

Utilize parse_url and then parse_str as follows:

<?php

    $url = "https://steamcommunity.com/tradeoffer/new/?partner=377361903&token=9Q0WYuq0";
    $result = parse_url($url);
    parse_str($result['query'], $var);

    echo $var['partner'];
    echo $var['token'];

Ideone example

References

1

You can do it this way

$url = parse_url("https://steamcommunity.com/tradeoffer/new/?partner=377361903&token=9Q0WYuq0");
parse_str($url['query'], $par);

$par is now a array with the data you want to collect, when giving a print_r in it:

Array
(
    [partner] => 377361903
    [token] => 9Q0WYuq0
)

To call just use $par["partner"] or $par["token"] to obtain access to array

Reference

Get a Youtube video ID from the URL

PHP - parse_url

PHP - parse_str

Browser other questions tagged

You are not signed in. Login or sign up in order to post.