首页 > 解决方案 > 如何删除数组前后的“”

问题描述

如何 " "从数组开始和数组结束后删除。我只需要数组而不需要"

  public function showPatientModal(Request $request)
    {

        $patient_id = $request->input('id');

        $data['patient'] = Patient::with('cases')->where('id', $patient_id)->first();

        $files_before = $data['patient']->file_before;
        $files_after = $data['patient']->file_after;

        dd($files_before);

    }

将以下内容返回给我,但我将数组作为字符串获取

"["download (2).jpg","download (3).jpg","download.jpg"]"

标签: php

解决方案


<?php
$string = '"["download (2).jpg","download (3).jpg","download.jpg"]"';
$string = preg_replace('/(?<=^)\s*"\s*(?=\[)/', '', preg_replace('/(?<=\])\s*"\s*(?=$)/', '', $string));
echo $string;

我没有对此进行测试,但它应该可以工作。

顺便说一句,如果你想将它传递给 javascript,你最好使用json_encode代替。


推荐阅读