htmlentities won't convert into html - php

I am learning PHP and I am trying to use
htmlentities but he won't print it as HTML
$string2 = '<h1>Hello</h1>';
echo htmlentities($string2);
he just print it as <h1>Hello</h1>
what I am doing wrong?

htmlentities aims to escape special characters to display them instead of interpreting them. If you want your browser interprets HTML tags, you just have to echo the HTML code.
$string2 = '<h1>Hello</h1>';
echo $string2;

Related

How to output a string containing text surrounded by angle brackets? [duplicate]

I want to print following text as it is:
echo "<label> AAAAA";
But it is just showing 'AAAAA' as output.
How can I escape '<' and '>' symbol.
Use htmlspecialchars.
<?php
echo htmlspecialchars("abc & < >");
?>
<?php
$string = "<label> AAAAA"; //whatever you want
echo htmlspecialchars($string);
?>
refrence htmlspecialchars
echo htmlentities("<label> AAAAA");
Use the htmlentities() function to convert into a plain text string.
<?php
echo htmlentities("<label> AAAAA");
?>
check this http://php.net/manual/en/function.htmlentities.php, and this is code -
echo htmlentities ("<label> AAAAA");
You should escape your especial characters for HTML.
echo "<label> AAAA"
http://www.w3schools.com/tags/ref_entities.asp
echo "<label> AAAAA";
Use HTML entities: < for < and > for >. Could be achieved using htmlspecialchars function: http://php.net/htmlspecialchars.
Read more about HTML entities here: http://www.santagata.us/characters/CharacterEntities.html

php - how to prevent echo &macr creates ¯ character,

I want to echo a string of an url variable:
echo "direct=1&closeBrowser=1&savelog=log.txt&storage=xfile&macrfile=klanta\test.html";
except the '&macr' creates the ¯ characters. How do i prevent this from happening?
htmlspecialchars function is used to convert special characters to HTML entities. See for reference, https://www.php.net/manual/en/function.htmlspecialchars.php
You can use this code :
$str = direct=1&closeBrowser=1&savelog=log.txt&storage=xfile&macrfile=klanta\test.html
echo htmlspecialchars($str);
OR you can use regular Expression :
echo preg_replace('/[^a-zA-Z0-9_ -]/s','', $str);
If you want to print a strings as raw text (ignoring any html special entities or tags), then use function htmlspecialchars($string).
Example:
echo htmlspecialchars($your_string);
Details: https://www.php.net/manual/en/function.htmlspecialchars.php

using fpdf : str_pad doesn't work with spaces, it works with other characters

I am adding a pad to my string, to fill with spaces, but it doesn't work
the code is here
<?php
$string1 = "Product 1 ";
$newString = str_pad($string1,100);
echo $newString."test";
echo "<br>";
$string2 = "Product 2222 ";
echo str_pad($string2,100," ")."test";
echo "<br>";
?>
the output is like this:
Product 1 test
Product 2222 test
You could try $str = str_pad($string2,(100*strlen(" "))," ")."test"; instead.
renders to a non-breaking-space in html (and when writing to document with fpdf).
Please note this can only work with fpdf when you tell it to write all lines as html! And the encoding should be utf-8 probably
$fpdf->Write(iconv('UTF-8', 'windows-1252', html_entity_decode($str)));
When the output of the PHP is converted to HTML, all the white spaces except the first are removed and it is the default feature of HTML and web browsers. so the output will not be correct.
You have to use the " " instead of white space in the str_pad function. HTML don't ignore the " " and against each existance of it, HTML adds a white space to the string.

php echo html tag so it is viewable (not interpreted as code)

I am currently trying to echo a text value from a variable which contains html-style tags. <...>
$string = "variable_name";
$tag_str = "<".$string.">";
echo $tag_str;
currently this echo's as nothing as it believes it is html code. How would I go about echoing <variable_name> to the page so it is viewable and not interpreted as code by the browser?
You'll have to html encode your output
$string = "variable_name";
$tag_str = "<".$string.">";
echo htmlspecialchars($tag_str);
The angle brackets (<>) are precisely what tells the browser that it should be treated as HTML code. Instead, output the HTML-encoded versions of those otherwise special characters:
$tag_str = "<".$string.">";
Alternatively, automate this process:
$tag_str = htmlspecialchars("<".$string.">");
Use highlight_string().See below code
$string = "variable_name";
$tag_str = "<".$string.">";
highlight_string($tag_str);

How to display XML in HTML in PHP?

I have a string with XML:
$string =
"
<shoes>
<shoe>
<shouename>Shoue</shouename>
</shoe>
</shoes>
";
And would like display it on my website like this:
This is XML string content:
<shoes>
<shoe>
<shouename>Shoue</shouename>
</shoe>
</shoes>
So I would like to do it:
on site, not in textbox
without external libraries, frameworks etc.
formatted with proper new lines
formatted with tabs
without colors etc., only text
So how to do it in plain and simple way?
If you just want a plain-text representation of your (pre-formatted) string, you can wrap it in HTML <pre/> tags and use htmlentities to escape the angle brackets:
<?PHP echo '<pre>', htmlentities($string), '</pre>'; ?>
you can use htmlentities(), htmlspecialchars() or some similar function.
It should work like that:
echo '<p>This is XML string content:</p>'
echo '<pre>';
echo htmlspecialchars($string);
echo '</pre>';
If it is a SimpleXMLobject
<pre>
<?php
echo htmlspecialchars(print_r($obj,true));
?>
</pre>
I searched for a solution to have slightly coloured output:
$escaped = htmlentities($content);
$formatted = str_replace('<', '<span style="color:blue"><', $escaped);
$formatted = str_replace('>', '></span>', $formatted);
echo "<pre>$formatted</pre>\n";

Categories