escape special chars after reading from mysql for output in php - php

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'; ?>

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

Trim() not work in PHP7

I wrote a short test code in PHP7:
<?php
$str1=' bigapple ';
echo strlen($str1);
trim($str1) ;//or trim($str1," ")
echo strlen($str1);
?>
But whenever I use trim on $str1 ,the strlen would be return 10.
Can someone tell me the reason why? I've been searching it but find nothing.
You need to store trim string to any variable or you need to print trim string as following:
echo strlen(trim($str1));
You have not assigned the trimmed value in another variable. Try as below :
<?php
$str1=' bigapple ';
echo strlen($str1).'<br>';
$str2 = trim($str1);
echo strlen($str2).'<br>';
?>

HTML,PHP - Escape '<' and '>' symbols while echoing

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

Echoing variables in PHP without calling them

Let's say I have something like this:
echo "This is a $variable";
echo "<?php echo $var; ?>";
How can I make it simply output:
This is a $variable
<?php echo $var; ?>
Instead of trying to parse the variables?
Use single-quotes rather than double-quotes:
$var = 'This is a $variable';
echo $var;
Output:
This is a $variable
Use single quotes
echo 'This is a $variable';
echo '<?php echo $var; ?>';
Use single-quotes instead of double-qoutes:
echo '<?php echo $var; ?>';
If you want to show it in a web browser, use htmlentities so it's not interpreted as a html tag:
echo htmlentities('<?php echo $var; ?>');
Use single quotes
echo 'my vars = $a, $b, $c';
or if you need double quotes escape $ sign with slash in front of it like:
echo "my vars = \$a, \$b, \$c";
or use chr(36) to replace $ sign (my fav :) ), or.... etc

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