cj333
cj333

Reputation: 2609

php - why mysql text contain `[` will damage html css format

I insert some text contain [ (only one parentheses, not include ] ), then when I use mysql query to the front php page, the [ will damage html css format.

$result = mysql_query("SELECT * FROM mytable Order By date DESC LIMIT 10");  
while ($row = mysql_fetch_array($result))
{
echo '<div class="text">'.$row['content'].'</div>';
}

I notice the html output source code.

before the [ text, the html is correct,

<div class="text">this is a test, no parentheses</div>

but after [ text, the html looks as

<div class=&#8221;text&#8221;>this is a test, contain [ </div>

How many symbols will cause the faults like this? and how to avoid? Thanks.

Upvotes: 0

Views: 147

Answers (3)

kevtrout
kevtrout

Reputation: 4984

I notice that your div class is being displayed &#8221;text&#82212; in your second example.
&#8221; is the html entity code for quotation mark. If your css is broken, I'd guess it's because the browser isn't interpreting your class declaration as "text" and therefor isn't applying the rules. Maybe it isn't the [, but some other factor that is replacing your quotation marks with the html entity that's breaking your css.

Upvotes: 0

sglessard
sglessard

Reputation: 3156

As robertpitt said, character [ as tag inner text value should not damage html. You have to check for something else.

Upvotes: 0

Joe Phillips
Joe Phillips

Reputation: 51150

Are you using htmlentities or htmlspecialchars anywhere? As a sidenote, you should probably be doing something like this as well:

echo '<div class="text">'.htmlspecialchars($row['content'], ENT_QUOTES).'</div>';

Upvotes: 3

Related Questions