0

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. Below are the two excerpts of code (from the same tpl file) and the results:

<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. It is displaying the html code when I echo it inside the script tag. 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?

user3167249
  • 1,082
  • 2
  • 14
  • 28
  • 1
    Did you take that HTML from the source code or a DOM inspector? I'd imagine that your original string has `&` in it as HTML and your DOM inspector is simplifying the view for you. – Quentin Apr 16 '15 at 14:43
  • 1
    @pbaldauf — Why? It's HTML encoded not URL Encoded. – Quentin Apr 16 '15 at 14:43
  • @Quentin: I am using a DOM inspector, but the link works fine when clicked but javascript location = url; does not – user3167249 Apr 16 '15 at 14:45
  • 1
    — That's the issue then. Your URL has already been converted from text to HTML, so it works fine in an HTML attribute but not in JavaScript. The best approach for dealing with that is to go to whereever you are getting the URL from and convert it to text there. Then convert it to HTML only when you are about to insert it into an HTML document. – Quentin Apr 16 '15 at 14:49
  • Quentin: Thank you for helping me better understand why this is happening. – user3167249 Apr 16 '15 at 15:57

2 Answers2

1

&amp; is a HTML entity - your browser parses and displays it. Some similar questions about decoding here and here. Have you tried html_entity_decode():

html_entity_decode — Convert all HTML entities to their applicable characters

So

url = "<?php echo html_entity_decode($links['current_path']['href']); ?>"
Community
  • 1
  • 1
sitilge
  • 3,687
  • 4
  • 30
  • 56
  • Yes, and that does work. i was trying to understand why I would need to use that in a javascript tag and not in a standard link tag – user3167249 Apr 16 '15 at 15:56
  • That is how the browser/clients should handle these elements. Quoting http://www.w3.org/TR/html4/types.html#type-cdata: Although the STYLE and SCRIPT elements use CDATA for their data model, for these elements, CDATA must be handled differently by user agents. Markup and entities must be treated as raw text and passed to the application as is. The first occurrence of the character sequence "" (end-tag open delimiter) is treated as terminating the end of the element's content. In valid documents, this would be the end tag for the element. – sitilge Apr 16 '15 at 17:51
0

Use the php function html_entity_decode

Example:

<a href="<?php echo html_entity_decode($links['current_path']['href']); ?>" role="button"><?php echo $btn['text']; ?></a>
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268