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
Related
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;
I have to convert these symbols:
> ;
✌ ;
...
etc.
to:
>
✌
How I can do it? Is anyone function in PHP to do it?
You can use PHP's html_entity_decode() function:
<?php
$str = ">";
$str = html_entity_decode($str);
$str1 = "✌";
$str1 = html_entity_decode($str1);
?>
More information at http://php.net/manual/en/function.htmlentities.php.
Note: html_entity_decode() function is the opposite of htmlentities() function.
Hope this helps, thanks!
<?php
$str = "This is some <b>bold</b> text.";
echo htmlspecialchars_decode($str);
?>
htmlspecialchars_decode
I am trying to output the results from a field that is read from mysql.
the field has this in it.
< ? echo "yes"; ? >
however when i try to print the field... it is null.
do i have to escape it on output?
i dont want to execute the code.. i want to output what is in the field as text
You have to escape with htmlspecialchars()
$val = htmlspecialchars ('<?php echo "yes"; ?>');
echo $val;
I guess htmlspecialchars is that what you want:
<?php
$str = "<?php echo 'lol'; ?>";
echo htmlspecialchars($str);
?>
it outputs <?php echo 'lol'; ?>
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
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";