简体   繁体   English

Base64使用PHP解码文本区域内的多行

[英]Base64 decode multiple lines within a textarea with PHP

I have a simple script where the user clicks a button and the code will execute a shell command that cuts base64 encrypted data from a csv and places it inside of a textarea. 我有一个简单的脚本,用户单击一个按钮,代码将执行一个shell命令,该命令从csv剪切base64加密的数据,并将其放置在textarea内。 Then, I simply assign that value to a variable and echo the decoded result. 然后,我简单地将该值分配给变量并回显解码的结果。 However, my result is nothing. 但是,我的结果什么都没有。 If I echo $encoded_data I do see the encoded strings. 如果我回显$ encoded_data,我确实会看到编码后的字符串。

My data is laid out like this within the textarea: 我的数据在文本区域中的布局如下:

ZGF2aWRAZW1haWwuY29t
ZGF2aWRAZW1haWwuY29t
ZGF2aWRAZW1haWwuY29t
ZGF2aWRAZW1haWwuY29t
...

There are hundreds of lines of encoded strings. 有数百行编码的字符串。

If I pass a string, the decoder works. 如果我传递字符串,则解码器可以工作。 But, with the text inside of the textarea it returns nothing at all. 但是,文本在文本区域内时,它什么也不返回。 I'm assuming the amount of strings passed causes a memory issue. 我假设传递的字符串数量导致内存问题。 I also noticed that if i try to pass multiple strings I get weird characters likely because of the line breaks. 我还注意到,如果我尝试传递多个字符串,我可能会因为换行而得到奇怪的字符。 How can I avoid this as well? 我又如何避免这种情况?

<form name="decode" action="" method="post" enctype="multipart/form-data">
<textarea id="decode_field" name="decode_field" rows="10" cols="50" autofocus><?
if (isset($_POST['decode_btn'])) {

       // Cut base64 data from csv file. Selected data is in col 2 of    each row.
       $encoded_data = shell_exec("cut -d, -f2 test.csv");

       // Display data to user.
       echo base64_decode($encoded_data);

}
?></textarea>
<input type="submit" name="decode_btn" value="Get Decoded Data" />
</form>

output should be the base64 strings decoded to regular strings line by line. 输出应该是逐行解码为常规字符串的base64字符串。

You want to use exec instead, which will allow you to get an array of lines of the output from your cut. 您想改用exec,这将使您能够从剪切中获取输出的行数组。

   $data = array();
   exec("cut -d, -f2 test.csv", $data);
   foreach($data as $line) {
       echo base64_decode($line);
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM