GET method... Extract variables - php

Can someone help me how to extract the url parameters from the following url using GET method in php?
http://domainname.com/?formBuilderForm%5BFormBuilderID%5D=29&formBuilderForm%5Brandomizer%5D=508398db941a26.20741366&formBuilderForm%5Bvw_sre_ticker_name%5D=TPX&formBuilderForm%5Bvw_sre_entry_price%5D=100&formBuilderForm%5Bvw_sre_entry_date%5D=21%2F10%2F2012
I believe the variable name is formBuilderForm [FormBuilderID] and value is 29. I tried the following code but it didn't work.
<?php
$_vw_sre_ticker_name_in = $_GET["formBuilderForm [FormBuilderID ]"];
echo 'vw_sre_ticker_name'.$vw_sre_ticker_name_in;
?>
I believe, it something to do with ASCII and Non-ASCII stuff.

%5D stands for [ in url encodings
So you could probably try
<?php
$_vw_sre_ticker_name_in = $_GET["formBuilderForm[FormBuilderID]"];
echo 'vw_sre_ticker_name'.$vw_sre_ticker_name_in;
?>
You needed to remove the spaces.
However as pointed out by Seth, this is really really basic debugging a simple
print_r($_GET);
Would have provided you with the information you needed rather than asking on Stackoverflow.

You can use foreach on $_GET
foreach($_GET as $key=>$value) {
echo $key . ':' . $value;
}

Related

Can not get the special charcter from query string value using PHP

I need one help. I need to fetch all data from query string using PHP but some special charcters like (i.e-+,- etc) are not coming. I am explaining my code below.
http://localhost/test/getmethod.php?name=Goro + Gun
Here I need to get the value assign to name using the below code.
<?php
$name=$_GET['name'];
echo $name;
?>
Here I am getting the output like Goro Gun but I need the original value i.e-Goro + Gun .Please help me to resolve this issue.
#subhra try this for this case name=Goro + Gun:
<?php
$nameArr = explode('=', $_SERVER['QUERY_STRING']);
$name = str_replace("%20", " ", $nameArr[1]);
echo $name;
?>
$_SERVER['QUERY_STRING'] - this will return you full query string

How to find and convert strings that are using curly brackets {site_name} for example in a HTML textarea output?

I am designing a Mail Template editor in my application. I need an idea to find the occurance of specific variables to be able to convert them using preg_replace or preg_match in my code.
For example, my template looks like this: (Sample only) this is what is returned from the textarea variable.
<p>Thank you for your order at {site_name}.</p>
In this example, I would like to replace the {site_url} with a specific variable or code from PHP since I can't parse PHP directly into the textarea.
Hope my question was clear, any help appreciated.
Edit: Is my question clear? I need to replace the {} strings using my own php code. The php code cann't be used in textarea directly. That is why i need to find a templating system that replace predefined variables {... } but convert them using php when interpreting the template.
Do you mean something like this?
<p>Thank you for your order at <?=$site_name?>.</p>
or
<p>Thank you for your order at <?php echo $site_name?>.</p>
edited:
Ahhh, do you mean something like this then:
$html = '<p>Thank you for your order at {site_name}.</p>';
$php_variables_array = array(
"site_url" => "http://www.google.co.uk",
"site_name" => "Google",
);
foreach ($php_variables_array as $key => $value)
{
$html = str_replace("{" . $key . "}", $value, $html);
}
echo $html;
<?php echo $site_name;?>

Assigning php POST array to Javascript array

I know this may sound similar to some past Q/As, I think mine is slightly different though.. I have a webpage which I want to dynamically load text file information. I upload the text file through an iframe and I want to save this information from php to Javascript. Whenever I try to save this as a regular variable it doesn't work so I have tried to do this by saving this information as a part of the $_POST array under a hidden form named $_POST['hidden_form']. Whenever I try to read the php into Javascript, I keep getting an error "Unexpected token ILLEGAL." I have tried the following two codes:
for($i=0;$i< count($_POST['hidden_form']) ;$i++)
{
echo "saved_form[$i]='" . $_POST['hidden_form'][$i]. "';\n";
}
and
saved_form = <?php echo json_encode($_POST['hidden_form']); ?>;
Assigning a php array into a javascript array
I think the error has to do with the " ' " needed to specify the array but not sure. I have no idea where to go from here so any help would be GREATLY appreciated. If there are better methods to do this please let me know. Thanks in advance!
saved_form = '<?php echo addslashes(json_encode($_POST['hidden_form'])); ?>';
Or
for($i=0;$i< count($_POST['hidden_form']) ;$i++)
{
echo "saved_form[$i]='" . addslashes($_POST['hidden_form'][$i]) . "';\n";
}
Both should work, probably had quotes breaking something?
the best way i have used is,
text/javascript
var saved_form = <?php echo json_encode($_POST['hidden_form']) ?>
Please note there are no Quotes around the php so your saved_form is an Object not a string json string witch would require you to to use var form_object = eval(saved_form)
#Lee might have meant this?
Just a note though i would not use the Raw $_POST pass it to a function that can loop though and addSlashes every value inside the post some thing like
<?php
function arr_addSlashes($array){
$ret = array();
foreach($array as $k => $v){
$ret[$k] = addSlashes($v);
}
return $ret;
}
?>

identify and execute php code on a string

I would like to know if it's possible to execute the php code in a string. I mean if I have:
$string = If i say <?php echo 'lala';?> I wanna get "<?php echo 'dada'; ?>";
Does anybody knows how?
[EDIT] It looks like nobody understood. I wanna save a string like
$string = If i say <?php count(array('lala'));?>
in a database and then render it. I can do it using
function render_php($string){
ob_start();
eval('?>' . $string);
$string = ob_get_contents();
ob_end_clean();
return $string;
}
The problem is that I does not reconize php code into "" (quotes) like
I say "<?php echo 'dada'; ?>"
$string = ($test === TRUE) ? 'lala' : 'falala';
There are lots of ways to do what it looks like you're trying to do (if I'm reading what you wrote correctly). The above is a ternary. If the condition evaluates to true then $string will be set to 'lala' else set to 'falala'.
If you're literally asking what you wrote, then use the eval() function. It takes a passed string and executes it as if it were php code. Don't include the <?php ?> tags.
function dropAllTables() {
// drop all tables in db
}
$string = 'dropAllTables();';
eval($string); // will execute the dropAllTables() function
[edit]
You can use the following regular expression to find all the php code:
preg_match_all('/(<\?php )(.+?)( \?>)/', $string, $php_code, PREG_OFFSET_CAPTURE);
$php_code will be an array where $php_code[0] will return an array of all the matches with the code + <?php ?> tags. $php_code[2] will be an array with just the code to execute.
So,
$string = "array has <?php count(array('lala')); ?> 1 member <?php count(array('falala')); ?>";
preg_match_all('/(<\?php )(.+?)( \?>)/', $string, $php_code, PREG_OFFSET_CAPTURE);
echo $php_code[0][0][0]; // <?php count(array('lala')); ?>
echo $php_code[2][0][0]; // count(array('lala'));
This should be helpful for what you want to do.
Looks like you are trying to concatenate. Use the concatenation operator "."
$string = "if i say " . $lala . " I wanna get " . $dada;
or
$string = "if i say {$lala} I wanna get {$dada}.";
That is what I get since your string looks to be a php variable.
EDIT:
<?php ?> is used when you want to tell the PHP interpreter that the code in those brackets should be interpreted as PHP. When working within those PHP brackets you do not need to include them again. So as you would just do this:
// You create a string:
$myString = "This is my string.";
// You decide you want to add something to it.
$myString .= getMyNameFunction(); // not $myString .= <?php getMyNameFunction() ?>;
The string is created, then the results of getMyNameFunction() are appended to it. Now if you declared the $myString variable at the top of your page, and wanted to use it later you would do this:
<span id="myString"><?php echo $myString; ?></span>
This would tell the interpreter to add the contents of the $myString variable between the tags.
Use token_get_all() on the string, then look for a T_OPEN_TAG token, start copying from there, look for a T_CLOSE_TAG token and stop there. The string between the token next to T_OPEN_TAG and until the token right before T_CLOSE_TAG is your PHP code.
This is fast and cannot fail, since it uses PHP's tokenizer to parse the string. You will always find the bits of PHP code inside the string, even if the string contains comments or other strings which might contain ?> or any other related substrings that will confuse regular expressions or a hand-written, slow, pure PHP parser.
I would consider not storing your PHP code blocks in a database and evaluating them using eval. There is usually a better solution. Read about Design Pattern, OOP, Polymorphism.
You could use the eval() function.

removing items from url strings in PHP

I would appreciate some help here:
What I want to do:
remove &itemsperpage=10 from:
http://localhost/thi/search/filter.html?type=featured&page=2&itemsperpage=10
and create a link from it:
http://localhost/thi/search/filter.html?type=featured&page=2&itemsperpage=15
here's what I have come up with so far:
<a href="<?php echo url::site(url::current()) . http_build_query($_GET) // don't know what follows ?>"
the framework functions I'm using are:
url::current() = returns current url in controller/action format
url::site() = returns absolute url i.e http://localhost/site/controller/action
so I have to remove '&itemsperpage' from the resulting string in the http_build_query function
but I am having trouble with character encodings and such! please help!
so here's the problem with character encoding:
$needle = '&itemsperpage';
$querystring = http_build_query($_GET) . '<br/>';
// echo $querystring . '<br/>';
$pos = strpos($querystring, $needle);
$remove = substr($querystring, ((int)$pos));
echo substr(str_replace($remove, '', $querystring), 1); // returns ';'
I can't remove the string '&itemsperpage' from the result of http_build_query which is:
'type=featured&page=2&itemsperpage=10' and functions like strstr outputs nothing
I would just do this:
$array = $_GET;
$array['itemsperpage'] = 15;
Then just use your code, but with the new variable (and the ?):
<a href="<?php echo url::site(url::current()) . '?' . http_build_query($array)">
The HttpQueryString class has several methods for getting, setting, modifying query strings and 'translating' their charsets.
You can achieve the effect you're looking for by removing the itemsperpage element from the $_GET array before building the query string.
unset($_GET['itemsperpage']);
And then just use the code you already wrote:
<a href="<?php echo url::site(url::current()) . http_build_query($_GET); ?>">
EDIT: I misread your post. I thought you only wanted to remove the field / value pair from the GET request. All you have to do is overwrite the value with the value you want:
$_GET['itemsperpage'] = 15;
And then use the code you already wrote.

Categories