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?

5
  • 1
    Did you take that HTML from the source code or a DOM inspector? I'd imagine that your original string has &amp; in it as HTML and your DOM inspector is simplifying the view for you. Commented Apr 16, 2015 at 14:43
  • 1
    @pbaldauf — Why? It's HTML encoded not URL Encoded. Commented Apr 16, 2015 at 14:43
  • @Quentin: I am using a DOM inspector, but the link works fine when clicked but javascript location = url; does not Commented Apr 16, 2015 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. Commented Apr 16, 2015 at 14:49
  • Quentin: Thank you for helping me better understand why this is happening. Commented Apr 16, 2015 at 15:57

2 Answers 2

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']); ?>"
Sign up to request clarification or add additional context in comments.

2 Comments

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
That is how the browser/clients should handle these elements. Quoting 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.
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>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.