PHP - Convert &#62 and other symbols to text - php

I have to convert these symbols:
&#62 ;
&#9996 ;
...
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

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

I want to echo php text but not between ()

I want to echo php text but not between (). Some thing like this =
<?php
$text = "Barry(male)";
echo $text;
?>
output =
Barry
How can i do this?
You can use preg_replace to substitute whatever is between parenthes (and the parentheses themselves) with an empty string. Like this:
<?php
$text = "Barry(male)";
echo preg_replace('#\(.*\)#', '', $text);
?>
Please note: since you didn't specify your string format, I'm assuming that the parenthesized text appears just once in the string and that there aren't nested parenthes. Otherwise, this doesn't work as expected.
Something like:
$text = "Barry(male)";
$split = explode("(", $text);
echo $split[0];
// "Barry"

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

PHP ent_quotes exception for <br> and <ar>

i am trying to covert HTML to entities using PHP, but i need to except <br> and <a> tags.
here's an example of my code
<?php
$string[0] = "<a href='http://hidd3n.tk'>Needs to stay</a> Filler text in between
<br><br> <script src='http://malicious.com/'></script> NEEDS to go";
$string[1] = htmlentities($string[0], ENT_QUOTES, "UTF-8");
?>
Let me suggest you to use a BBCode which will be way more safe.
EDIT:
Okay i have worked out a way.
Take this function rather safe than previous one:
function convert_myhtml_entities($string){
$string = htmlentities($string, ENT_NOQUOTES, "UTF-8");
$string = preg_replace('/<\s*br\s*(\/|)\s*>/U','<br$1>',$string);
$string = preg_replace('/<\s*a(.*)\s*>/U','<a$1>',$string);
$string = preg_replace('/<\s*\/\s*a\s*>/U','</a>',$string);
return $string;
}
now it is the tested with the string above.

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