html entities

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
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

html entities

Post by s.dot »

I have a database field that may or may not contain html

One instance may be

Code: Select all

<a href="#">link here</a>
and another instance may be

Code: Select all

<a href="#">link here</a>
My problem comes when displaying these in a textarea field. Obviously I want to show the html as text, so in both cases the html should show up as

Code: Select all

<a href="#">link here</a>
I'm not sure how to do this, but I was thinking something along these lines

Code: Select all

$text = htmlentities(html_entity_decode($databasetext,ENT_QUOTES),ENT_QUOTES);
echo "<textarea>$text</textarea>";
Is there any case where this would mess up? For instance if the user wrote out an entity as text like " but the message was stored in HTML.

Would this still show up fine?

I can't seem to wrap my mind around the idea.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Lets say that the message is stored in HTML

but the part of it was typed in as entities, like this

Code: Select all

<a href="#">stuff</a>

<a href="#">stuff</a>
Now, when I pull this out into the textarea, I would like it to look exactly like that. With that current way of doing it it shows up like this:

Code: Select all

<a href="#">stuff</a>

<a href="#">stuff</a>
However if its stored in entities instead of HTML it comes out like its supposed to

Code: Select all

<a href="#">stuff</a>

<a href="#">stuff</a>
I hope I am making sense.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

OK I think I have it solved

Code: Select all

echo '<textarea>';
if($approve_a['pref_profile_comment_html'] == 1){
    //  it was stored in entities, so just show it
	echo $cment;
} else {
    //  it was stored in html, so show the entities
	echo htmlentities($cment,ENT_QUOTES);
}
echo '</textarea>';
Does that look correct?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply