douyouyi8878 2015-05-15 10:40
浏览 16
已采纳

在php中爆炸文件中的字符串

As i am new to the PHP.I am trying to explode the strings from the file.I want only some part of the strings to display in the html fields. So here is my complete code:-

<?php 
 if(isset($_POST["Submit"]))
{
    $lhs= array();
    $rhs= array();
    foreach($_POST as $key => $value){
        if($key == "Submit") continue;
        echo $key ."=". $value ;
        echo "<br>";
        $lhs[]=$key; //first array for left hand side 
        $rhs[]=$value; //second array for right hand side
        }
    
    file_put_contents("file2.txt", implode(PHP_EOL, array_map(function($v1, $v2)
    {
      return "$v1=$v2";
    },$lhs, $rhs)));
}
$file_contents=file_get_contents("file2.txt");
$data=explode("=" ,$file_contents);
print_r($data);
?>

    <form name="form1" method="post" action="">
    Name: <input type="text" name="name" value="<?php echo "$data[0]";?>"><br>
    Phone No: <input type="text" name="phone" /><br/>
    Course:<input type="text" name="course" /> <br />
    <input type="submit" name="Submit" value="Sign Up">
    </form> 
    

And the output would be like this

name=xyz
phone=12334
course=bba
Array ( [0] => name [1] => xyz phone [2] => 12334 course [3] => bba )

Now i want to display only values like

 xyz
 12334
 bba

In the respective html fields.And if i edit some values in file that also as to update in the html fields when i refresh/reload the page. And i am not sure this approach is correct or not to achieve my requirement.

Please any help or advice that will be appreciated.Thanks in advance.

</div>
  • 写回答

3条回答 默认 最新

  • ds2321 2015-05-15 10:54
    关注
    $str=implode(",",$data);
    
    $str1=str_replace(" ",",",$str);
    $output_array=explode(",",$str1);
    print_r($output_array);
    

    This will give you array like this

    Array
    (
        [0] => name
        [1] => xyz
        [2] => phone
        [3] => 12334
        [4] => course
        [5] => bba
    )
    

    Now You Can use this array

    Further You can make it Assocaitive Array Like this

    $arr_values=array();
    for($i=0;$i<sizeof($output_array);$i++)
    {
    $arr_values[$output_array[$i]] = $output_array[++$i];
    }
    print_r($arr_values); 
    

    this will Give you out put like this

    Array
    (
        [name] => xyz
        [phone] => 12334
        [course] => bba
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集