Charlie
Charlie

Reputation: 129

Quotes Within Quotes Issues

I'm trying to create a textbox that will be displayed on my website. When displayed, I'd like to show some data within the text box. Here is what I have

echo "<input type=\"text\" size=\"100\" value=\"<a href=\"$url\"></a>\">";

All that shows up in the text box is <a href= And then at the end of the text box, right after the text box I see ">

I know something must be syntactically off, just not sure what.

Upvotes: 0

Views: 138

Answers (4)

Tadeck
Tadeck

Reputation: 137450

You have made some mistake. Your code will result in something like that (also visible in this jsfiddle):

<input type="text" size="100" value="<a href="http://www.google.com/"></a>">

Instead you can use something like that:

echo "<input type=\"text\" size=\"100\" value=\"&lt;a href=&quot;$url&quot;&gt;&lt;/a&gt;\">";

or

echo '<input type="text" size="100" value="&lt;a href=&quot;' . $url . '&quot;&gt;&lt;/a&gt;">';

to receive effect visible in this jsfiddle. Is it satisfying enough?

Upvotes: 0

Tomasz Kowalczyk
Tomasz Kowalczyk

Reputation: 10477

You must encode <, ", and > chars - they can't be embedded that way. Use:

echo '<input type="text" size="100" value="'.htmlspecialchars('<a href="'.$url.'"></a>').'">';

You may also use urlencode() function - see which suits you better.

One more tip - use single quotes when string contains HTML-like content. This will save you adding \" everywhere.

Upvotes: 2

trutheality
trutheality

Reputation: 23455

Think of what the html would look like:

<input type="text" size="100" value="<a href="$url"></a>">
                                             ^
                                             |
                                             This is where the value attribute ends!

htmlspecialchars should solve it.

Upvotes: 0

Dmitri Gudkov
Dmitri Gudkov

Reputation: 2133

php_code ?>
<input type="text" size="100" value="<a href=&quote;<?=$url;?>&quote;></a>\">
<?php
php_code

maybe this will work for you

Upvotes: 0

Related Questions