7

In PHP (Wordpress theme function, trying to add html stored in theme options to blog header), I'm trying to get the following line:

$x="<p>html</p>"; echo $x;

To render html just like:

echo "<p>html</p>";

The results are different, the first one will display html tags while the second will process the html. Can someone please help. Thanks

Dung
  • 19,199
  • 9
  • 59
  • 54
john
  • 81
  • 1
  • 1
  • 5
  • 2
    They both render the same result : http://codepad.viper-7.com/vzRPxP – Baba Oct 02 '12 at 15:17
  • 1
    There's no functional difference between the two. PHP doesn't care if you echo out a fixed string or the contents of a variable - it does not "interpret" html. – Marc B Oct 02 '12 at 15:17
  • 1
    you need it raw - filter marks the value as being "safe", which means that in an environment with automatic escaping enabled this variable will not be escaped if raw is the last filter applied to it. In your case because your framework environment escaped the string hence html tags are displayed. – Dung May 21 '16 at 18:10

2 Answers2

26

A. If you want to show the HTML Tags you can just use htmlentities

Example

$x = "<p>html</p>";
echo htmlentities($x);

Output

<p>html</p>

B. If you want the other way round its possible your string is stored as &lt;p&gt;html&lt;/p&gt; that is why you are seeing <p>html</p> then you should use html_entity_decode

Example

$x = "&lt;p&gt;html&lt;/p&gt;";
echo html_entity_decode($x);

Output

html

C. It could be you are not using a web broswer and you want html then you should use strip_tags

Example

$x = "<p>html</p>";
echo strip_tags($x);

Output

html
HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
Baba
  • 94,024
  • 28
  • 166
  • 217
  • Sorry I'm looking for the other way around, I only need the output to be the text : html – john Oct 02 '12 at 15:25
  • can you `var_dump(get_field())` or where you are getting the info from let me see if you are getting the write string format – Baba Oct 02 '12 at 15:27
  • I got this: string(23) "

    hh

    "
    – john Oct 02 '12 at 15:29
  • if you `echo get_field()` you should get `hh` not `

    hh

    ` if you are access it via a web browser
    – Baba Oct 02 '12 at 15:30
  • I'm still getting

    hh

    with the code: echo get_field("general_header_code", 'option'); even on the example you mentioned above on code pad, I'm getting different results
    – john Oct 02 '12 at 15:33
  • use `strip_tags` ans see the output .... – Baba Oct 02 '12 at 15:34
  • Still not working. Sorry. I'm still getting

    s. The code I eventually want to output is actually google adsense code, so not sure about strip tags.

    – john Oct 02 '12 at 15:37
  • There is nothing you are doing wrong .. you can add your code to pastebin let me see if i can help – Baba Oct 02 '12 at 15:39
  • Here is the full code http://pastebin.com/umQKXNN8 the otput when using var_dump is string(23) "

    hh

    "
    – john Oct 02 '12 at 15:45
  • It worked, appears to be a cache problem with custom fileds. Thank you for your time. – john Oct 02 '12 at 15:58
6

Use single quotes

Single quotes vs double quotes in PHP

echo '<p>HTML</p>';
Community
  • 1
  • 1
csi
  • 9,018
  • 8
  • 61
  • 81
  • 1
    I'm actually fetching the variable through another function get_field(), I over simplified the example above I guess. – john Oct 02 '12 at 15:20