【问题标题】:htmlspecialchars() expects parameter 1 to be string, array given in Laravelhtmlspecialchars() 期望参数 1 是字符串,Laravel 中给出的数组
【发布时间】:2018-08-04 08:50:02
【问题描述】:

我的Laravel刀片模板中有这个错误

htmlspecialchars() 期望参数 1 是字符串,给定数组

我尝试将数组转换为刀片模板中的字符串。

这里是代码

<script>
    var value = {{ $sliderImageDataArray }}.toString()
    window.MedicPressSliderCaptions = value;
</script>

其中$silderImageDataArray 是这里存储的变量和值。

这是$silderImageDataArray

[{"title":"First title","text":"<p><span id=\"hs_cos_wrapper_module_1498510869515998\" class=\"hs_cos_wrapper hs_cos_wrapper_widget_container hs_cos_wrapper_type_widget_container\" data-hs-cos-general-type=\"widget_container\" data-hs-cos-type=\"widget_container\">First title<\\\/span><\\\/p>\n<p><a class=\"btn  btn-secondary\" href=\"http:\\\/\\\/localhost\\\/sencare\\\/book-appoinment\\\/\" target=\"_self\">  Make An Appointment <\\\/a>\\u00a0<a class=\"btn  btn-light\" href=\"http:\\\/\\\/localhost\\\/sencare\\\/our-doctors\\\/\" target=\"_self\"> Our Doctors<\\\/a><\\\/p>\n","is_video":false},{"title":"Second","text":"<p><span id=\"hs_cos_wrapper_module_1498510869515998\" class=\"hs_cos_wrapper hs_cos_wrapper_widget_container hs_cos_wrapper_type_widget_container\" data-hs-cos-general-type=\"widget_container\" data-hs-cos-type=\"widget_container\">Second<\\\/span><\\\/p>\n<p><a class=\"btn  btn-secondary\" href=\"http:\\\/\\\/localhost\\\/sencare\\\/book-appoinment\\\/\" target=\"_self\">  Make An Appointment <\\\/a>\\u00a0<a class=\"btn  btn-light\" href=\"http:\\\/\\\/localhost\\\/sencare\\\/our-doctors\\\/\" target=\"_self\"> Our Doctors<\\\/a><\\\/p>\n","is_video":false}]

【问题讨论】:

    标签: php html laravel laravel-5.3 laravel-blade


    【解决方案1】:

    {{}} 将被 Blade 模板引擎转换为 echo()。而且您正在尝试将数组作为字符串回显。

    您可以将其转换为 JSON:

    var value = '{{ json_encode($sliderImageDataArray) }}';
    

    如果是 Laravel 集合或模型:

    var value = '{{ $sliderImageData->toJson() }}';
    

    【讨论】:

    • 第一个对我来说是正确的。现在没有错误。但我没有得到目标结果
    • @raff 您的问题是关于错误的。如果您想将数据转换为某种格式,您应该已经描述了您究竟是如何获取数据的以及您想要获得的确切结果。
    • 这是正确的答案,但是刀片引擎将 {{ $x }} 替换为 &lt;?php echo htmlspecialchars( $x ); ?&gt; 而不是 &lt;?php echo ( $x ); ?&gt;
    猜你喜欢
    • 2018-02-22
    • 2018-11-10
    • 2017-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-03
    • 2017-07-12
    • 2017-03-22
    相关资源
    最近更新 更多