tags:

views:

45

answers:

3

Hi everybody,

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

$data = array("Name"=>'<div style="color:red"></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.

+4  A: 

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.

Ignacio Vazquez-Abrams
Yes but it will display the html source code ... is there a cleaner way to do it?
Zakaria
@Zakarai — What do you want it to do? Parse the HTML and then strip out anything that isn't a text node?!
David Dorward
+1  A: 

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

nikic
+6  A: 

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.

w3d