oshirowanen
oshirowanen

Reputation: 15925

ampersand issues

I have a link stored in my database, lets say it's

http://somedomain.com/page.php?username=tom&surname=smith

When I bring this back to html, I get an xhtml error because of the &

How do I convert the & to an &

Also, I have some links in the database which already have & instead of &

So, how do I convert & when required, but not convert & to &

Upvotes: 3

Views: 1527

Answers (3)

shmeeps
shmeeps

Reputation: 7833

The easiest way would be do replace all & to &, and then all & to $#38;, such as

$content = str_replace("&", "&", $content);
$content = str_replace("&", "$#38;", $content);

But you should not store data like that in your database.

Upvotes: 0

JD Isaacks
JD Isaacks

Reputation: 57974

You can use htmlentities.

Upvotes: 1

Marc B
Marc B

Reputation: 360702

Don't store escaped/encoded text. You never know what format the text will be required to be in when you retrieve it. If you store it URL-encoded, but you have to insert it into an HTML document,then you have to undo the url encoding and switch to HTML encoding. Which is a waste. Best to store it in 'raw' format and convert as needed at the time you need it.

Use htmlspecialchars() to escape/encode the XML/HTML metacharacters prior to inserting into your XML document.

Upvotes: 6

Related Questions