PHP自动编码&在 Wordpress 中 [英] Php automatically encoding & in Wordpress
问题描述
已解决
包装 & 的每个实例带有 htmlspecialchars() 的 php 中的字符通过输出 &
而不是 &
解决了我的问题,并允许下一页正确识别每个变量!
Wrapping each instance of the & character in the php with htmlspecialchars() fixed my problem by outputting &
instead of &
and allowed the next page to recognize each of the variables correctly!
/已解决/
我目前正在一个 Wordpress 网站上工作,并试图在 php 中创建一些 javascript,然后将其回显到页面上.这个 javascript 的一部分使用 window.open() 和我在 php 中生成的 url 打开一个新窗口/选项卡.但是,当我回显包含 url 的变量时,所有 &
字符都会自动编码为 &
.
I am currently working on a Wordpress site and am trying to create some javascript in the php and then echo it onto the page. Part of this javascript opens a new window/tab using window.open() and a url I have generated in the php. However, when I echo the variable containing the url, the all the &
characters are auto encoded as &
.
以下是正在发生的事情的示例:
Here is an example of what is happening:
...
?>
<script type="text/javascript">
var url = "&";
var url2 = "<?php echo "&"; ?>";
var url3 = <?php echo json_encode("&"); ?>;
var url4 = "<?php echo html_entity_decode("&"); ?>";
var url5 = "<?php echo html_entity_decode("&"); ?>";
</script>
<?php
...
然后,当我检查插入脚本的元素时显示的实际 javascript 是
Then the actual javascript that is shown when I inspect the element where the script is being inserted is
...
<script type="text/javascript">
var url = "&";
var url2 = "&";
var url3 = "&";
var url4 = "&";
var url4 = "&";
</script>
...
我已经尝试使用 urlencode()-ing 所有 & 的 url,但是 $_GET 无法将它们识别为 url 变量.
I have tried urlencode()-ing all of the &'s out of the url, but then $_GET does not recognize them as url variables.
这只是 Wordpress 的东西,还是 php 的东西?还有,我能做些什么让 &'s 显示为只是 &'s 吗?!
Is this just a Wordpress thing, or is it a php thing? And, is there anything I can do to make the &'s show up as just &'s?!
编辑
由于我试图做的事情似乎有些混乱,让我进一步解释一下.我正在使用 php 而不是 javascript 创建一个 url.然后我使用 php 生成一些 javascript,然后将其回显到页面上.以上摘录仅显示了我实际面临的问题.实际的鳕鱼段看起来像这样:
Since there seems to be some confusion on what I'm trying to do, let me explain further. I am creating a url using php, not javascript. I am then generating some javascript using php and then echoing that out onto the page. The above excerpt only shows the problem that I am actually facing. The actual cod segment looks something like this:
$header_element .= "<script type=\"text/javascript\">\n"
. "function validateForm(form) {\n"
. "\tvar isInvalid = false;\n"
. $str_all_validation
. "\tif (!isInvalid) {\n"
. "\t\trunReport(form);\n"
. "\t} else {\n"
. "\talert('You must enter all report parameters.');\n"
. "\t}\n"
. "}\n"
. "function runReport(form) {\n"
. "\twindow.open('/?page_id=172&PageId=$str_page_id&ReportName=$str_report_name"
. "$str_all_param, '_blank')\n"
. "}\n"
. "</script>";
除了拥有所有 & 之外,上述输出正确无误.字符输出为 &
.
The above outputs correctly aside from having all of the & characters output as &
.
推荐答案
我认为您的页面或 php 的编码混淆了.
I think your encoding for either your page or your php is mixe up.
尝试在您的 php 中添加这一行:
try adding this line in your php:
header('Content-Type: text/html; charset=UTF-8');
这应该确保您使用 php 输出的任何内容都是 UTF-8.
this should make sure that whatever you output using php is in UTF-8.
并确保您的 javascript 编码相同.
And make sure your encoding for your javascrip is the same.
PS 您可以更改 charset=UTF-8
部分,例如 charset=ISO-8859-1
或您在 javascript 中使用的任何编码.
PS You can change the charset=UTF-8
part for something like charset=ISO-8859-1
or whatever encoding your using in your javascript.
不要使用最后一个答案,不是编码问题!!!
DO NOT USE THE LAST ANSWER NOT AN ENCODING PROBLEM!!!
尝试用 htmlspecialchars()
这篇关于PHP自动编码&在 Wordpress 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!