I would like to print only specific part from an Url like for example if i got on response an URL like this :
I want to print only this part exactly :
XRpC8tmn2_VUz6temt_j19BIGymiYyd5ZOLoWtp1H5U
I have tried to use substr like this :
$string=$headers['location'][0];
$substring = substr($string, strpos($string, '=') + 1);
// Use substr() again to get the part of the string before the semicolon
$final_string = substr($substring, 0, strpos($substring, ';'));
file_put_contents("xxx", $final_string);
$xtoken = file_get_contents("xxx");
but it’s doesn’t print nothing to my file xxx
but if I apply it like this only :
$string=$headers['location'][0];
file_put_contents("xxx", $string);
$xtoken = file_get_contents("xxx");
it print full line as I mentioned above .
Hope I find the right guide to be able to select & print only that specific part I want .
thank you .
2
Answers
Perhaps a combination of parse_url and then parse_str could help without needing
substr
?As an example:
Gives the output:
The example is here if you’d like to try a test run.
EDIT: from the comment about the file:
(will not be able to write to a file in the online example)
Then to the file can be
file_put_contents("xxx", $output['session']);
, or whatever one would prefer to call$output
if a different name is desired.… based on your original code with:
… could change to:
If you are working with the URL, Just use the super global $_GET. In your question you are needing the string that pertains to the session. So to get that string
To access the other parameters you would use: