逃脱]字符串中的字符

时间:2017-01-10 23:04:34

标签: php laravel blade

我有一个嵌套循环,我用它来生成选项表单元素。我正试图让我的输出

round_1 [
  'playername' => 'id'
]

我相信我现在的代码是无意义的,因为括号在传递到<select name=...语句时没有被转义。其他一切都很好。

有没有办法逃脱$ player并将其传递到PHP或Blade中的数组?

我已粘贴了相关的代码块:

{!! Form::open(['url' => 'admin/games/process']) !!}

@foreach ($players_array as $game)
    <h4>Round {{$round}}</h4>
    <div class="form-group">

        @foreach($game as $player)
            <p><strong>{{ $player }}</strong></p>

            <!-- This chunk might be able to be written cleaner using laravelcollective/html -->
            <select name="round_{{$round}}[{{$player}}]">
                @foreach($all_players as $key=>$p)
                    <option value="{{ $key }}">{{ $p }}</option>
                @endforeach
            </select>

        @endforeach
    </div>
    <!-- {{ $round++ }} -->

@endforeach

<div class="form-group">
    {!! Form::submit('Submit Refactored Players', ['class' => 'btn btn-outline-primary form-control']) !!}
</div>
{!! Form::close() !!}

谢谢!

1 个答案:

答案 0 :(得分:0)

解决方案是使用str_replace替换]字符,将其传递给控制器​​,然后创建一个新数组,将旧数组放入反向替换,因为您无法可靠地更改php密钥

查看

@foreach ($players_array as $game)
    <h4>Round {{$round}}</h4>
    <div class="form-group">

        @foreach($game as $player)
            <p><strong>{{ $player }}</strong></p>

            <!-- This chunk might be able to be written cleaner using laravelcollective/html -->
            <select name="round_{{$round}}[{{str_replace("]", "fstupidkey", $player)}}]">
                @foreach($all_players as $key=>$p)
                    <option value="{{ $key }}">{{ $p }}</option>
                @endforeach
            </select>

        @endforeach
    </div>
    <!-- {{ $round++ }} -->

@endforeach

控制器

public function process_logs(Request $request)
{
    /*
     * Accept player match arrays from user form
     * Input event data into database
     */


    $a = $request->round_1;

    /*
    * Create new array to put shit in..
    */
    $b = [];

    foreach($a as $key=>$item)
    {
        $c = str_replace("fstupidkey", "]", $key);
        $b[$c] = $item;
    }

    dd($b);

}