1

I have basic html stored in my database, which includes html such as the anchor tag.

I am storing all & in the database as &

However, when the data is displayed back on the screen, the anchor tags stop working because the & in the links become &.

The problem is, the rest of the content around the anchor tag should remain as &

For example

The database content might look like this

<p>this is paragraph 1 & this is <a href="http://www.companyname.com/page.php?paragraph=2&sentence=4">paragraph 2</a>.</p>

The & in the url becomes &#38; which stops the url from working, but at the same time, the & in the

tab any any other tag should remain as &#38;

oshirowanen
  • 15,297
  • 82
  • 198
  • 350

3 Answers3

2

Both the & in the href and in the <p> should be encoded as &amp; to be valid

Source: http://htmlhelp.com/tools/validator

Twelve47
  • 3,924
  • 3
  • 22
  • 29
1

Probably you are looking for http://pl.php.net/manual/en/function.html-entity-decode.php

I assume you are storing the url separately in db.

See also: http://pl.php.net/manual/en/faq.html.php

Szymon Lukaszczyk
  • 712
  • 1
  • 6
  • 14
  • No, the urls are in the content, i.e. i have a content column in the database, and all text and urls within the text go into that single column for content. Just like your answer, the links are within the sentences. – oshirowanen Apr 20 '11 at 10:21
  • Than it becomes quite tricky. I think your WYSIWYG editor makes the conversion before putting into db. You can use some kind of regexp query to extract the url and change it. Or just use str_replace to change `&` into `&` – Szymon Lukaszczyk Apr 20 '11 at 10:25
  • They shouldn't be decoded. Entities *should* be encoded in HTML. – Lightness Races in Orbit Apr 20 '11 at 10:28
  • But shouldn`t this be encoded as `&` not as `&` ? see: http://htmlhelp.com/tools/validator/problems.html#amp – Szymon Lukaszczyk Apr 20 '11 at 10:38
0

The URL should still "work".

Although common convention is not to bother, HTML entities such as ampersands should always be encoded like &amp; or &#38; — you're already doing the latter, so the resulting URL should be valid.

e.g. <a href="page.php?a=1&amp;a=2">link</a> and <a href="page.php?a=1&#38;a=2">link</a> both present an anchor tag linking to the URI page.php?a=1&a=2.


Update Ah, apparently in some browsers the hash in &#38; is seen as ending the URL.

What a mess! Use &amp; instead of &#38; ... or, ideally, %26.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055