SyntaxError:PHP变量中未终止的字符串文字(SyntaxError: unterminated string literal in PHP variable)

编程入门 行业动态 更新时间:2024-10-24 06:30:02
SyntaxError:PHP变量中未终止的字符串文字(SyntaxError: unterminated string literal in PHP variable)

我搜索了有关“未终止的字符串文字”语法错误的许多问题,但没有发现任何帮助我...

所以,我有这个Javascript函数:

function parametresModal($parametres) { document.getElementById('remodalTest').innerHTML = $parametres; };

然后我在我页面的链接上调用此函数:

<a href="#" onClick='parametresModal("<?php the_field('description-defi'); ?>");'>TEST</a>

这里写的参数是简化的; 我实际上想要添加这个Wordpress ACF的功能和HTML标记,但我发现问题出现在这个特定领域(见下文)。

这个“parametresModal”函数应该用它的参数填充下面的div:

<div id="remodalTest">MyDiv</div>

问题是控制台输出

“SyntaxError:unterminated string literal”

Wordpress ACF的字段“description-defi”包含几行文字和一些简单的引号(例如c'est,l'éviter,...)。 所以我尝试用几种方法来逃避引号:

$myField = the_field('description-defi'); $myEscape = json_encode($myField);

要么

$myField = the_field('description-defi'); $myEscape = addshlashes($myField);

要么

$myField = the_field('description-defi'); $myEscape = htmlspecialchars($myField);

总是导致相同的错误。

你看到我的代码或我的思维方式可能出错吗?

非常感谢你 !

I search through the numerous questions already asked about the "unterminated string literal" syntax error but found nothing helping me ...

So, I have this Javascript function :

function parametresModal($parametres) { document.getElementById('remodalTest').innerHTML = $parametres; };

Then I call this function on a link in my page :

<a href="#" onClick='parametresModal("<?php the_field('description-defi'); ?>");'>TEST</a>

The parameter written here is simplified ; I actually want to add this Wordpress ACF's function among others and HTML markup, but I found the issue was appearing with this particular field (see below).

This "parametresModal" function is supposed to fill the following div with its parameters :

<div id="remodalTest">MyDiv</div>

Problem is the console outputs

"SyntaxError: unterminated string literal"

The Wordpress ACF's field "description-defi" contains a few lines of text with some simple quotes (ex. c'est, l'éviter, ...). So I tried to escape the quotes with several methods :

$myField = the_field('description-defi'); $myEscape = json_encode($myField);

or

$myField = the_field('description-defi'); $myEscape = addshlashes($myField);

or

$myField = the_field('description-defi'); $myEscape = htmlspecialchars($myField);

Always resulting in the same error.

Do you see where I could be wrong in my code or my way of thinking the thing ?

Thank you very much !

最满意答案

the_field()将输出所选字段的内容。 如果要使用字段,则应使用get_field() 。

请参阅: https : //www.advancedcustomfields.com/resources/the_field/

此外,换行字符也不会被任何PHP的转义函数转义,如果你的String包含换行符,你需要使用以下内容手动转义它们: $myField = str_replace(array("\r\n", "\n"), "<br>", $myField); 。

如果您知道您的数据库将始终使用相同的换行符序列,则可以使用该换行符序列替换array("\r\n", "\n") 。

the_field() will output the content of the selected field. If you want to work with a field, you should use get_field() instead.

See: https://www.advancedcustomfields.com/resources/the_field/

Also the newline character will not be escaped by any of PHP's escape functions, if your String contains newlines, you will need to escape them manually using something like this: $myField = str_replace(array("\r\n", "\n"), "<br>", $myField);.

If you know that your DB will consistently use the same newline sequence, you can replace array("\r\n", "\n") by that newline sequence instead.

更多推荐

本文发布于:2023-07-28 19:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1308178.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   变量   中未   文字   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!