Неожиданный токен при сохранении переменной PHP в переменной Javascript - PullRequest
0 голосов
/ 05 апреля 2019

У меня есть следующая функция на моем сайте Wordpress, чтобы поставить класс на идентификатор, когда дата верна.Проблема заключается в том, что разрыв строки создается в переменной Javascript, когда он читает файл, который вы ранее создали с помощью идентификаторов.

function ProgramarBorrado(){

    //Get day, month and year
    $date = getdate();
    $day = $date['mday'];
    $month = $date['mon'];
    $year = $date['year'];

    //Create a list
    $list = array();

    //Open/create a file
    $myfile = fopen("lista.txt", "w+");

    //If true push an ID on a list
    if(($day==5)&&($month==4)&&($year==2019)){
        array_push($list,"#borrar22marc");
    }

    if(($day==5)&&($month==4)&&($year==2019)){
        array_push($list,"#prova");
    }

    //For each value of the list, write in the file lista.txt
    foreach ($list as $value) {
        fwrite($myfile, $value."\n");
    }

    //Close write mode
    fclose($myfile);

    //Open read mode
    $myfile = fopen("lista.txt", "r");

    //Get the value of each line of the file
    while(!feof($myfile)) {
      ?>
            <script>
                //Save the PHP variable on a JS variable
                var simple = '<?php echo fgetss($myfile) ;?>';
                console.log(simple);
                //Add class with jQuery
                jQuery(simple).addClass('borrar-programado');
            </script>
        <?php
        }

}
add_action('wp_footer', 'ProgramarBorrado');

Это ошибка:

                //Save the PHP variable on a JS variable
                var simple = '#borrar22marc
';

1 Ответ

0 голосов
/ 05 апреля 2019

Да!Функция обрезки является решением, благодаря 04FS за решение, вот как выглядит код после решения:

function ProgramarBorrado(){

    //Get day, month and year
    $date = getdate();
    $day = $date['mday'];
    $month = $date['mon'];
    $year = $date['year'];

    //Create a list
    $list = array();

    //Open/create a file
    $myfile = fopen("lista.txt", "w+");

    //If true push an ID on a list
    if(($day==5)&&($month==4)&&($year==2019)){
        array_push($list,"#borrar22marc");
    }

    if(($day==5)&&($month==4)&&($year==2019)){
        array_push($list,"#prova");
    }

    //For each value of the list, write in the file lista.txt
    foreach ($list as $value) {
        fwrite($myfile, $value."\n");
    }

    //Close write mode
    fclose($myfile);

    //Open read mode
    $myfile = fopen("lista.txt", "r");

    //Get the value of each line of the file
    while(!feof($myfile)) {
      ?>
            <script>
                //Save the PHP variable on a JS variable
                var simple = '<?php echo trim(fgets($myfile)) ;?>';
                console.log(simple);
                //Add class with jQuery
                jQuery(simple).addClass('borrar-programado');
            </script>
        <?php
        }

}
add_action('wp_footer', 'ProgramarBorrado');
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...