htmlenitities() html_entity_decode()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
amirbwb
Forum Commoner
Posts: 89
Joined: Sat Oct 30, 2010 6:10 pm

htmlenitities() html_entity_decode()

Post by amirbwb »

hi everyone, i am creting a comment box so i want users to be allowed to insert symbols like : ♥♣♠• but in the the same time i don't want to get a field box when they insert <input type="text">
for example if they entered this --> hello ♥ <input type="text">

the result :
-with nl2br(htmlentities())
hello � <input type="text">

-withoutit or with nl2br(html_entity_decode())
hello ♥ [_____________________] (with a field where i can write)

I hope u understand the prob
thanks for helping :D
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: htmlenitities() html_entity_decode()

Post by social_experiment »

htmlspecialchars() ?
(sic) php manual
The translations performed are:
'&' (ampersand) becomes '&'
'"' (double quote) becomes '"' when ENT_NOQUOTES is not set.
''' (single quote) becomes ''' only when ENT_QUOTES is set.
'<' (less than) becomes '<'
'>' (greater than) becomes '>'
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
amirbwb
Forum Commoner
Posts: 89
Joined: Sat Oct 30, 2010 6:10 pm

Re: htmlenitities() html_entity_decode()

Post by amirbwb »

if i use htmlspecialchars()

it gives me:
<input> & #9829; (I have added the space between "&#...;" because the page is transforming the code into symbole)

what i want is <input> ♥

here is my code for testing:

Code: Select all

<?php
$a=$_POST['a'];
?>
<form method="post">
<input name="a" value="<?php echo $_POST['a'];?>">
</form>
<?php 
echo htmlspecialchars($a);
?>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: htmlenitities() html_entity_decode()

Post by social_experiment »

strip_tags() is an option. It removes html and php tags but you have an option to allow certain tags. This should leave the symbols displaying as symbols. You could also use a function like str_replace to replace all html-unsafe characters and replace them with html-safe characters.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: htmlenitities() html_entity_decode()

Post by phphelpme »

I think the best option here would be strip_tags()

http://php.net/manual/en/function.strip-tags.php

Best wishes
User avatar
amirbwb
Forum Commoner
Posts: 89
Joined: Sat Oct 30, 2010 6:10 pm

Re: htmlenitities() html_entity_decode()

Post by amirbwb »

mmm ok ... but I'll have lot of code to add .. anw thks both of u =)
Post Reply