如何将原始POST数据解析为数组? [英] How to parse raw POST data into array?
本文介绍了如何将原始POST数据解析为数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个原始的表单数据,如下所示:
I have a raw form-data that look like this:
------------V2ymHFg03ehbqgZCaKO6jy
Content-Disposition: form-data; name="intro"
O
------------V2ymHFg03ehbqgZCaKO6jy
Content-Disposition: form-data; name="title"
T
------------V2ymHFg03ehbqgZCaKO6jy
Content-Disposition: form-data; name="apiKey"
98d32fdsa
------------V2ymHFg03ehbqgZCaKO6jy
Content-Disposition: form-data; name="method"
/media/add
------------V2ymHFg03ehbqgZCaKO6jy
Content-Disposition: form-data; name="upload_field"; filename="original_filename.png"
Content-Type: image/png
------------V2ymHFg03ehbqgZCaKO6jy--
(代替upload_field的第二行,是该文件的数据(在此处不可见).所以我的问题是:
(In place of second line of upload_field there are data of this file (invisible here). So my question is:
如何解析以上数据以创建表格:
How to parse above data to have a table:
$result['intro']
以此类推,里面有数据?
and so on with data inside?
推荐答案
$boundary = "------------V2ymHFg03ehbqgZCaKO6jy"; // take this from content-type
$rawfields = explode($boundary,$data);
array_pop($rawfields); // drop the last -- piece
foreach ( $rawfields as $id=>$block )
{
list($mime,$content) = explode("\r\n\r\n",$block,2); // I think it's <cr><lf> by standards, maybe check!
if ( preg_match('/name="([^"]*)"/i',$mime,$match) )
{
$result[$match[1]] = $content;
// todo: do funky stuff with other fields
} else {
$result[] = $content; // just in case...
}
}
presto.
编辑:您还应该修剪每个内容块中的换行符,但是rtrim
会砍掉一个以上的换行符,因此您必须多一点创意.
edit: you should also trim off the newline from each content block, but rtrim
will chop more than one newline, so you have to get a little more creative.
这篇关于如何将原始POST数据解析为数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文