php的报价转换为'变成javascript

时间:2019-01-28 16:47:15

标签: javascript html symfony chart.js

我需要在php中构建的特定格式,然后将其发送到我的视图和javascript(使用symfony,但这不是symfony的问题)。

问题在标题中指明:来自字符串的'被转换为我的js,但我不希望对其进行转换

这是它的样子

Php端

        case "01":
            $dynamic_month="['Février ".$previous_year."','Mars ".$previous_year."','Avril ".$previous_year."','Mai ".$previous_year."
            ','Juin ".$previous_year."','Juillet ".$previous_year."','Août ".$previous_year."','Septembre ".$previous_year."','Octobre 
            ".$previous_year."','Novembre ".$previous_year."','Décembre ".$previous_year."','Janvier ".$year."']";
            break;

这正在构建一个类似['Month Year','NextMonth Year']的字符串(这就是var_dump所打印的内容。

但是在我的JavaScript中(树枝视图中的chart.js)。 '被替换。

var chart = new Chart(ctx, {
                // The type of chart we want to create
                type: 'line',

                // The data for our dataset
                data: {
                    labels: {{ dynamic_month }},
                    datasets: [{
                        label: "Connexion / mois",
                        backgroundColor: 'rgb(255, 99, 132)',
                        borderColor: 'rgb(255, 99, 132)',
                        data: connectedByMonth,
                    }]
                },

                // Configuration options go here
                options: {}
            });

问题已经解决

labels: {{ dynamic_month }}

一旦被此编译,它将被替换

labels: ['Février 2018','Mars 2018', et caetera et caetera

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

看看以下转义引号:Escaping quotation marks in PHP

如果这行不通,您可以随时在事实发生后进行str.replace并将其正确转换回去

答案 1 :(得分:0)

您要在PHP端设置dynamic_month变量,但要在Javascript中使用它;您需要替换:

labels: {{ dynamic_month }},

使用

labels: {<?php echo $dynamic_month ?>},

HTH