Scott
Scott

Reputation: 51

urlencode/urldecode problem

I am selecting data from a MySql database and doing the following

echo "<input type=\"text\" name=\"distadd1\" id=\"distadd1\" class=\"textbox\" value=".  urlencode($row['ADD1']). " >";

however the value is being displayed as follows Department+of+Planning+%26+Development.

how do i remove the + and %26

Upvotes: 1

Views: 721

Answers (2)

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146588

Don't add those symbols in the first place. The urlencode() function, as its name suggests, is used to generate URLs. You probably want to use htmlspecialchars().

I tried htmlentities but it just returns the first word of the string.

Your HTML is invalid. You need to use quotes so your attributes can contain spaces. Compare:

<input name=user value=foo bar id=foo>
<input name="user" value="foo bar" id="foo">

Upvotes: 0

Tommy
Tommy

Reputation: 39807

URL encoding is different than what you want to do it sounds like. Try changing URL encode to htmlentities, which will get rid of the '+' and the &26; but will still protect you from XSS / script injection into your webpages.

Example #1 A htmlentities() example
<?php
$str = "A 'quote' is <b>bold</b>";

// Outputs: A 'quote' is &lt;b&gt;bold&lt;/b&gt;
echo htmlentities($str);

// Outputs: A &#039;quote&#039; is &lt;b&gt;bold&lt;/b&gt;
echo htmlentities($str, ENT_QUOTES);
?>

From the PHP Manual

Upvotes: 2

Related Questions