How to split a link into "various parts"? [duplicate]

1

I have the following link:

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

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

How can I do this?

    
asked by anonymous 01.04.2017 / 02:32

2 answers

1

Use 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

01.04.2017 / 02:41
1

You can do this as follows

$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 by giving print_r to it:

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

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

Reference

  

Get ID of a video from YouTube by URL

     

PHP - parse_url

     

PHP - parse_str

    
01.04.2017 / 02:44