开发者

Turning off the html interpretation in php

I have a problem with getting some html value stored in a php array:

$data = array("Name"=>'<div style="color:r开发者_运维问答ed"></div>');
while(list($key,$val) = each($data)){
  print_r($key." => ".$val) ;
}

The problem is that the script (put inside html tags) returns:

Name =>
Foo Bar

So, the div (so the html) is interpretated. What I want to display is the value, not its interpretation. So, the result that I want is:

Name => <div style="color:red">Foo Bar</div>

Is there a way to do that?

Thank you very much,

Regards.


If you want to display the actual HTML as stored in your array, then you need to convert the special characters into HTML entities using htmlentities(), or htmlspecialchars(). ie. you need to convert < into &lt; to display the character correctly on the page.

print_r($key." => ".htmlentities($val)) ;

htmlentities() converts all characters that have HTML entity equivalents. htmlspecialchars() converts just the essentials.


This is the browser interpreting the HTML, not PHP. Simply send the page with a content type of text/plain and the browser will not try to interpret it.


You may want to htmlspecialchars your output, so the HTML tags get replaced by entities.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜