skarama
skarama

Reputation: 497

Escaping double quotes in php

I have the following variable being dynamically set by user generated content:

$variable = '<a href="http://www.mysite.com/article">This Article</a>';

When this variable is set, I then echo it as such

echo $variable;

I know that as is, this wouldn't be valid because I would need to escape the double quotes etc.

Is there a way to automate the process to make the variable printable as a clickable link, thus escaping the quotes in my variable automatically?


Edit: Turns out this is indeed perfectly valid, but this being used on a joomla site, html tags are being stripped out, and I have to use [[a href]] instead of the regular <>. Thanks everyone for your help!

Upvotes: 1

Views: 8323

Answers (4)

TALLBOY
TALLBOY

Reputation: 1077

Your code looks fine and should be functional.

Upvotes: 0

darthnosaj
darthnosaj

Reputation: 261

I tried to use your exact code in a php page I have and it acts as you need it to. Seems to be working just fine for me.

Upvotes: 2

Tomalak
Tomalak

Reputation: 338118

To display the string "as it is" in a browser, you must pass it through htmlspecialchars().

echo htmlspecialchars($variable);

If you don't, the browser interprets the HTML and displays the link text, as expected.

Upvotes: 7

Brian Adkins
Brian Adkins

Reputation: 666

I don't see a problem with your code as-is... You're using single quotes to contain the entire string, so it should output as a clickable link when viewed in a browser

Upvotes: 2

Related Questions