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";
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 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 (). 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"
I have a content like below.
I want the <pre></pre> to be converted to <p> </p>. But i'm not able achieve it. Below is an example
$content = "<pre>This is a pre text
I want to convert it to paragraphs ";
print_r(str_replace(array('<pre>', '</pre>', '
'),array('<p>', '</p>', '<br/>'),htmlspecialchars($content)));
But i get the output as it is. Can someone help me resolve. Thanks in advance
You are changing $content before replacing the string.
$content = "<pre>This is a pre text
I want to convert it to paragraphs ";
print_r(htmlspecialchars($content));
// returns <pre>This is a pre text I want to convert it to paragraphs
None of which matches your str_replace
Remove the htmlspecialchars() and you will get the output you wanted.
$content = "<pre>This is a pre text
I want to convert it to paragraphs ";
print_r(str_replace(array('<pre>', '</pre>', '
'),array('<p>', '</p>', '<br/>'),$content));
//returns <p>This is a pre text <br/> I want to convert it to paragraphs
your code should be like this to getting expected output.
<?php
$content = "<pre>This is a pre text </pre>
I want to convert it to paragraphs";
print_r(htmlspecialchars(str_replace(array('<pre>', '</pre>', '
'),array('<p>', '</p>', '<br/>'),$content)));
?>
You can get your desired output without removing the htmlspecialchars() function by doing something like this:
$content = "<pre>This is a pre text
I want to convert it to paragraphs ";
print_r(str_replace(array('<pre>', '</pre>', ' '),array('<p>', '</p>', '<br/>'),htmlspecialchars($content)));
i use (str_replace) function to replace ##ID## in youtube url with this regular expression : (?P<id>[a-z-A-Z_0-9]+)
so i use this code to do this :
<?php
$urlbase = 'https://www.youtube.com/watch?v=##ID##';
$lastchange = str_replace('##ID##', '(<id>[a-z-A-Z_0-9]+)', $urlbase);
echo $lastchange;
?>
i get the output in the browser like this : https://www.youtube.com/watch?v=(?P[a-z-A-Z_0-9]+), its looks like <id> not show up !
i try this simple code :
<?php
echo "This is my <id>";
?>
but i just get this is my in the browser !
What's the probleme ? and how i can fix it , thanks
is being interpreted as HTML so your browser is parsing it and since it is not a renderable element, it shows nothing. Try:
<?php
echo "This is my <id>
?>
As for the str_replace, it's doing exactly what the function is supposed to be doing. If you're looking to use regular expressions in string replacements, use preg_replace
The tag <id> is being removed by your browser. It is really there if you watch the source code. Maybe you should try:
$urlbase = 'https://www.youtube.com/watch?v=##ID##';
$lastchange = str_replace('##ID##', '(<id>[a-z-A-Z_0-9]+)', $urlbase);
echo urlencode( $lastchange );
Problem is with the line:
$lastchange = str_replace('##ID##', '(<id>[a-z-A-Z_0-9]+)', $urlbase);
str_replace does not use regex.
You will need preg_replace
$pattern = '(<id>[a-z-A-Z_0-9]+)'
$replacement = '##ID##'
$string = $urlbase
$lastchange = preg_replace($pattern, $replacement, $string);
Also < and > are html entities which means they are reserved chars for HTML they have some special meanings if you want to show them then you must use there entity name eg < and > in your case respectively.
<?php
echo " echo "This is my <id>";
?>