Jump to content

Recommended Posts

I'm trying to take the variable from a url in text form. ex.:

<?php
$url = http://www.example.com/test.php?foo=bar&test=1

I'm basically looking for a function or a way to extract the variables or at least one of them. I've tried extract, but for some reason it won't work...

Link to comment
https://forums.phpfreaks.com/topic/121961-extract-_get-variable/
Share on other sites

i wrote this a while ago

function get_get($url)
{
$data = substr($url, strpos($url, '?') + 1);
$data = explode('&', $data);
foreach ($data as $value) {
	$value = explode('=', $value);
	$get[($value[0])] = $value[1];
}
return $get;
}

 

Scott.

$url = "http://www.example.com/test.php?foo=bar&test=1";
function get_get($url)
{
$data = substr($url, strpos($url, '?') + 1);
$data = explode('&', $data);
foreach ($data as $value) {
	$value = explode('=', $value);
	$get[($value[0])] = $value[1];
}
return $get;
}
print_r(get_get($url));

outputs

Array
(
    [foo] => bar
    [test] => 1
)

 

Scott.

 

Or if you want variables instead of the array:

<?php

$data = substr($url, strpos($url, '?') + 1);
$data = explode('&', $data);
foreach ($data as $_value) {
     $_value = explode('=', $_value);
     ${$_value[0]} = $_value[1];
}

echo $foo . " " . $test;
?>

 

That script won't work if _value is a name for the GET field. (The one before the = sign.)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.