Let's say that I have the following simple input text box:
<input type="text" name="Details[0]->Name" value="" />
Now the problem is, php translating the input name as array, and then ignore the rest name after the closing square bracket. So in print_r, it become:
Details => Array{
[0] => "Input"
}
What can I do to workaround this? Is there any unparsed $_REQUESTS?
N.B: If you noticed it, yes I am trying to use automatic input to class mapper as it has been done in Asp.net mvc.
Edit:
The additional solution requirement is that I can read raw array input from either GET, POST or multipart form requests.
For POST:
echo urldecode ( file_get_contents('php://input'));
For GET:
echo urldecode ($_SERVER['QUERY_STRING']);
Both the above give the output Details[0]->Name=testval
As for enctype='multipart/form-data', the unparsed data is not available in php. However, there is a solution of sorts given by this SO question: Get raw post data
Just replace Input with some another name like Inputcust and make new array
Details => Array{
[0] => "Inputcust"
}
and where ever you want to use again replace from Inputcust => Input
Related
I am trying to modify a php file (It is of Joomla extension Community Builder 1.9.1, and the file is \components\com_comprofiler\plugin\templates\default\default.php), in order to extract the e-mail address from a variable.
For description’s sake, let’s say this variable is $html. To make sure this variable is the right one containing the e-mail address that I'm targeting, I insert:
<pre><?php print_r($html) ?></pre>
Into the file, and its output is the email address with a mailto link, and the corresponding HTML is something like
<span id="cbMa47822" class="cbMailRepl">myemail#yahoo.com</span>
So I guess I can use:
<?php $html_array = explode("\"",$html);echo $html_array[5]; ?>
Io get 'mailto:myemail#yahoo.com'; But actually it only returns a notice of:
undefined offset:5
So I print_r($html_array), and it return something like
Array
(
[0] => cbMa14768
[2] => class=
[3] => cbMailRepl
[4] => >...
)
It looks like the <a> tag part of the html output is replaced by "...", like what you see in Chrome’s developer tool html inspector, where before you expand it, the HTML looks like:
<span id="cbMa47822" class="cbMailRepl">...</span>
I looked deeper into the php code, trying to find out how this $html is contructed, but it is totally beyond my understanding.
For learning purpose, my questions are:
why there is no [1] in the result of print_r($html_array)
How do I test a variable’s value more exactly, by more exactly I mean totally without html input, like if the value is "foo", if should display the HTML as is, but not a link (when I use print_r, it returns a link)?
And most importantly, based on the information given above, can you give my any hint regarding how I can extract the e-mail address from a variable like this?
Finally, for those who are willing to take a deeper look into this, the variable I am talking about is $this->tableContent[$userIdx][1][6]->value in \components\com_comprofiler\plugin\templates\default\default.php, originally it wasn't in the code but I did some test and confirm it contains the email address. I inserted the following code between line 450 & 451
<?php $html_array = explode("\"",$this->tableContent[$userIdx][1][6]->value);echo $html_array[5]; ?>
To extract an e-mail address from an HTML strcuture as you describe, just use regex and preg_match:
$html = '<span id="cbMa47822" class="cbMailRepl">myemail#yahoo.com</span>';
preg_match("/mailto:(.*)\">/is", $html, $matches);
echo '<pre>';
print_r($matches);
echo '</pre>';
The output would be:
Array
(
[0] => mailto:myemail#yahoo.com">
[1] => myemail#yahoo.com
)
So to access that e-mail address, just do this:
echo $matches[1];
The output would be:
myemail#yahoo.com
To avoid links you can use escape sequence.
you can use regular expression to match if the given string matches the email address pattern and print it
PHP has a vast support for functions which can perform wierdest tasks so search for them
I'm building a website that store json data to hidden input element with php
<input type='hidden' class='json_data' name='json_data' value='".json_encode($data[0])."'>
with that code, I have this result:
<input class="json_data" type="hidden" value="[{"ALBUM_ID":"1234","PHOTOS_ID":"1234578"}]" name="json_data">
but when I try to get the value with jquery.val and trying to show ALBUM_ID, i get this {
anything wrong with my way of putting json into html correctly?
and then get it with jquery / javascript ?
thanks
First go ahead at this open console and see the result. Ctl+Shift+j.
http://jsfiddle.net/techsin/Q2MHA/
You need to do two things fix. ' and "'
Second just this code
JSON.parse($('.json_data').val())[0]
you need [0] because for some reason your json object is wrapped in []..you would know why.
Your html should look like this
<input ... value='[{"ALBUM_ID":"1234","PHOTOS_ID":"1234578"}]'...>
You need to correctly handle entities in your input's value. If you populate it with PHP, use htmlspechalchars() and use result from this function
inspect the following line carefully.
<input class="json_data" type="hidden" value="[{"ALBUM_ID":"1234","PHOTOS_ID":"1234578"}]" name="json_data">
As you see you have used " for your string enclosement. The json string also includes " which breaks your string enclosement. Use ' to enclose the string.
<input class="json_data" type="hidden" value='[{"ALBUM_ID":"1234","PHOTOS_ID":"1234578"}]' name="json_data">
Try to escape the " with addslashes or htmlspecialchars
or encode the string with base64 and decode it with JS before parsing the string as JSON
When this monster is sent via POST
<input name="arrayname[index][string[name]]" value="321" />
this is what i get:
print_r($POST['arrayname']);
>> Array ( [index] => Array ( [string[name] => 321) )
Where is the secons bracket?
I can access it via:
echo $val['string[name'];
but thats just horrible.
The structure of the name should be kept if possible.
Its part of a large generic method and only in some cases theese names get generated. If its not possible to work with names like theese properly (it is but ugly like i mentioned above), i will have to change the whole form generating.
Incorrect : arrayname[index][string[name]]
Correct : arrayname[index][string][name]
You have to choose:
Let PHP convert your POST data to arrays using the square bracket syntax
Parse POST data yourself from raw post data
You just cannot have both at the same time.
<input name="arrayname[index][string][name]" value="321" />
I'm trying to use brackets within the name of a cookie.
It is supposed to look like this(this is how the browser wants it!):
Name: name[123456].newName
Content: 20
Here is my example:
$cookie = "name[123456].newName=20"
But when I analyze what the browser sees, I get this:
cookie['name'] = Array
And I want:
cookie['name[123456].newName'] = 20
My question is: How should I write the cookies name in a way that the browser understands?
Thank you in advance.
Actually, all you have to do is this:
<?php
setcookie('name[123456].newName', 20);
?>
This generates the following header:
Set-Cookie: name[123456].newName=20
... and browsers (well, at least Firefox) seem to handle it just fine.
The issue starts when you want to read the value back. PHP has an otherwise nice feature: whenever it finds an input parameter (get, post, cookie...) with square brackets on its name, it'll build an array from it. So print_r($_COOKIE) displays this:
Array
(
[name] => Array
(
[123456] => 20
)
)
I'm not aware of any way to disable this feature so you probably need to use string functions and parse the contents of the raw cookie, which can be found at $_SERVER['HTTP_COOKIE']:
name[123456].newName=20
I am trying to split a text into an array using explode, but for some reason that does not work when the text is coming from a posted form.
If I run explode('|§|', 'qwe|§|asd|§|zxc'); I will get an array like:
Array
(
[0] => qwe
[1] => asd
[2] => zxc
)
BUT
If this input text comes from a form define like:
<form method="post">
Input: <input type="text" name="query" size="50" value="qwe|§|asd|§|zxc"><input type="submit" value="Parse">
</form>
I am getting the following array:
Array
(
[0] => qwe|§|asd|§|zxc
)
Im guessing this has to do with iso settings and that the text in the 'query' field has been altered in some way, but I can't understand how to fix. I have tried setting <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1" /> and other charsets, but to no avail.
Any ideas? Thanks in advance.
Just an idea: The § sign is probably be converted to url format. Try urldecode() the string first.
I'm probably mistaken on this, but § may be a unicode character, which PHP does not yet support. Thus, there may be some issues when transferring from the form to the script.
Have you tried changing it to something more... normal? Like if you did qwe|~|asd|~|zxc instead, or maybe qwe|+~+|asd|+~+|zxc if you're concerned about what somebody would enter