Php automatically encoding & in Wordpress

2019-07-26 07:58发布

SOLVED

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!

/SOLVED/

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("&#038;"); ?>";
</script>
<?php
...

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 = "&#038;";
    var url2 = "&#038;";
    var url3 = "&#038;";
    var url4 = "&#038;";
    var url4 = "&#038;";
</script>
...

I have tried urlencode()-ing all of the &'s out of the url, but then $_GET does not recognize them as url variables.

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?!

EDIT

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 &#038;.

1条回答
2楼-- · 2019-07-26 08:14

I think your encoding for either your page or your php is mixe up.

try adding this line in your php:

header('Content-Type: text/html; charset=UTF-8');

this should make sure that whatever you output using php is in UTF-8.

And make sure your encoding for your javascrip is the same.

PS You can change the charset=UTF-8 part for something like charset=ISO-8859-1 or whatever encoding your using in your javascript.

EDIT:

DO NOT USE THE LAST ANSWER NOT AN ENCODING PROBLEM!!!

try and replace html_entity_decode() with the htmlspecialchars()

查看更多
登录 后发表回答