简体   繁体   English

PHP问题回显到javascript中

[英]Issues with PHP echo into a javascript

I am using the same line of code to echo a link onto my page in two locations and getting different results. 我使用同一行代码在两个位置将链接回显到页面上,并获得不同的结果。 One is inside an < a > tag and the other is inside a < script > tag. 一个在<a>标记内,另一个在<script>标记内。 Below are the two excerpts of code (from the same tpl file) and the results: 以下是两个代码摘录(来自同一tpl文件)和结果:

<a href="<?php echo $links['current_path']['href']; ?>" role="button"><?php echo $btn['text']; ?></a>

Results in: 结果是:

<a href="index.php?route=module/jw/list&token=a4c693a4e38916fc03af23ad4fe17188" role="button">Filter</a>

However in the script tag I have 但是在脚本标签中

url = "<?php echo $links['current_path']['href']; ?>"

And my result is 我的结果是

 url = "index.php?route=module/jw/list&amp;token=a4c693a4e38916fc03af23ad4fe1"

Notice the '&' after the route parameter. 注意route参数后面的“&”。 It is displaying the html code when I echo it inside the script tag. 当我在script标记内回显它时,它正在显示html代码。 I know I can convert it in the second instance, but I am curious as to why I would need to. 我知道我可以在第二种情况下进行转换,但是我对为什么需要转换感到好奇。 Why is the same php command making the symbol echo differently in various parts of the source code? 为什么相同的php命令在源代码的各个部分中使符号回声不同?

&amp; is a HTML entity - your browser parses and displays it. 是HTML实体-您的浏览器解析并显示它。 Some similar questions about decoding here and here . 关于此处此处的解码的一些类似问题。 Have you tried html_entity_decode(): 您是否尝试过html_entity_decode():

html_entity_decode — Convert all HTML entities to their applicable characters html_entity_decode —将所有HTML实体转换为它们的适用字符

So 所以

url = "<?php echo html_entity_decode($links['current_path']['href']); ?>"

Use the php function html_entity_decode 使用php函数html_entity_decode

Example: 例:

<a href="<?php echo html_entity_decode($links['current_path']['href']); ?>" role="button"><?php echo $btn['text']; ?></a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM