tags:

views:

233

answers:

4

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

$variable = '<a href="http://www.mysite.com/article"&gt;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!

+2  A: 

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

BrianAdkins
A: 

Your code looks fine and should be functional.

TALLBOY
If the string was started with single quotes, double quotes do not need escaping. And vice versa.
Tomalak
that doesn't escape the double quotes because it's no surrounded in double quotes
Galen
This will actually display the backslashes. The only character you need to escape inside single quotes is a single quote.
danieltalsky
+3  A: 

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.

Tomalak
+2  A: 

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.

darthnosaj