What does <? php echo ("<pre>"); ..... echo("</pre>"); ?> mean? - php

The question is the tag <pre> </pre>
I've seen one script I am working on, uses it:
echo ("<pre>");
....
....
echo ("</pre>");
What exactly does it do ?
Is it an Html tag or a PHP ?
I've searched on Google but nothing much comes out of it. When do we usually use that HTML tag?...or PHP tag?

The <prev> tag doesn't exist, but it's probably the <pre> HTML tag to put around debug output, to improve readability. It's not a secret PHP hack. :)

I think you're talking about <pre></pre>. element is displayed in a fixed-width font, and it preserves both spaces and line breaks.
try printing an array with a **<pre>** and whitout **<pre>**
$arr = array(1, 2, 3);
echo '<pre>';
print_r($arr);
echo '</pre>';
print_r($arr);

echo (""); is a php code, and <prev> tries to be HTML, but isn't.
As #pekka said, its probably supposed to be <pre>

It is nor php nor html it sounds like specific xml tag.

The PHP function echo() prints out its input to the web server response.
echo("Hello World!");
prints out Hello World! to the web server response.
echo("<prev>");
prints out the tag to the web server response.
echo do not require valid HTML tags. You can use PHP to print XML, images, excel, HTML and so on.
<prev> is not a HTML tag. Is is a valid XML tag, but since I don't know what page you are working in, i cannot tell you what it is. Maybe it is the root tag of a XML page, or a miswritten <pre> tag.

try this:
$names = array('Jesse', 'joey', 'jelnny', 'justine');
$names = new ArrayObject($names);
echo '<pre>';
print_r($names);
vs this:
$names = array('Jesse', 'joey', 'jelnny', 'justine');
$names = new ArrayObject($names);
//echo '<pre>';
print_r($names);
and it shows what the PRE does very neatly

The <prev>-tag might be an XML-tag.

if you put text within an HTML tag, all spaces and line breaks will be preserved.
Otherwise, the default behaviour is to remove multiple spaces and keep only one space, and ignore line breaks (unless you use the tag).
I'd like to say it's from the time before CSS - not necessarily used to make text more legible, only more formatted.

"<pre>" is an HTML tag. If you insert this line of code in your program
echo "<pre>";
then you will enable the viewing of multiple spaces and line endings. Without this, all \n, \r and other end line characters wouldn't have any effect in the browser and wherever you had more than 1 space in the code, the output would be shortened to only 1 space. That's the default HTML. In that case only with <br> you would be able to break the line and go to the next one.
For example,
the code below would be displayed on multiple lines, due to \n line ending specifier.
<?php
echo "<pre>";
printf("<span style='color:#%X%X%X'>Hello</span>\n", 65, 127, 245);
printf("Goodbye");
?>
However the following code, would be displayed in one line only (line endings are disregarded).
<?php
printf("<span style='color:#%X%X%X'>Hello</span>\n", 65, 127, 245);
printf("Goodbye");
?>

The <pre> is used to define pre-formatted text.
The text within <pre> tag is displayed in a fixed-width font and it preserves both spaces and line breaks that are present in the text.Here I'm printing a JSON FILE without <pre> tag and then with <pre> tag.
Without <pre> tag
https://i.stack.imgur.com/ofRn8.jpg
With <pre> tag
https://i.stack.imgur.com/XzDVg.jpg

$testArray = [
[
"name" => "Dinesh Madusanka",
"gender" => "male"
],
[
"name" => "Tharaka Devinda",
"gender" => "male"
],
[
"name" => "Dumidu Ranasinghearachchi",
"gender" => "male"
]
];
print_r($testArray);
echo "<pre>";
print_r($testArray);

here whatever we write in between the pre tags
it will be interpreted same as html pre tag
ex:
<?php
echo '<pre>';
echo '
code here
will be displayed
as it
is
namaste
';
echo "this line get printed in new line";
echo "</pre>";
echo "Now pre ended:";
echo "this line gets joined to above line";
?>
and content b/w 's font also changes.

Related

Is there a way in PHP array to show line break? [duplicate]

This question already has answers here:
PHP - how to create a newline character?
(15 answers)
Closed 4 years ago.
I have an index.php file and an array which has message. Is there a way that instead of <br> tag I can display the text with a new line in PHP so I can also store it in database?
The code:
$array = array(
array(
'id' => 1,
'message' => 'I\'m reading Harry Potter!',
),
array(
'id' => 2,
'message' => 'Ok. I just got a notification that you sent me a pin on Pinterest.<br>Will you come to school tomorrow?',
)
);
For example:
Ok. I just got a notification that you sent me a pin on Pinterest.
Will you come to school tomorrow?
The new line character is \n. Simply replace <br> with \n and you will have the results you're looking for.
PHP - how to create a newline character?
Note!
php does not process escape characters within single quotes.
'\n' is not processed as a new line character, while "\n" is.
What is the difference between single-quoted and double-quoted strings in PHP?
Depending on your platform, you may want to be more specific about which new line sequence you choose.
\r\n, \r and \n what is the difference between them?
$array = array(
array(
'id' => 1,
'message' => "I'm reading Harry Potter!"
),
array(
'id' => 2,
'message' => "Ok. I just got a notification that you sent me a pin on Pinterest.\nWill you come to school tomorrow?"
)
);
echo "<pre>";
print_r($array);
exit;
This will show you the raw formatting of your string.
To then convert new lines to the <br> tag for display on a webpage, you would pass that string to nl2br()
<?php echo nl2br($array[1]['message']); ?>
Is there a way that instead of <br> tag I can display the text with a new line in PHP
Yes you can easily do this with CSS
white-space: pre;
https://www.w3schools.com/cssref/pr_text_white-space.asp
Back in the day I used to do the whole "replace" thing, then I got bored of it. Now I just use CSS.
The pre option/setting will preserve whitespace much like using the <pre> tag. The only thing you have to watch for is indenting in the source code
<p style="white-space:pre;">
<?php echo $something; ?>
</p>
This extra space in the code will be added to the PHP output, instead do this:
<p style="white-space:pre;"><?php echo $something; ?></p>
You can close the php tag and reopen after the break.
For example -
'message'=>'Ok.I just got a notification that you sent me a pin on Pinterest. ?>
<br>
<?php Will you come to school tomorrow?',

Output mysql as raw text

I have a result in the database like this
[border color=#EEE]
[pictitle]Title of your picture[/pictitle]
[image]http://tingle.fm/wiki/assets/images/default-thumb.gif[/image]
[line color=#D4D4D4][/line]
[/border]
And for some reason when I output the text it shows like this:
[border color=#EEE]
[pictitle]Title of your picture[/pictitle]
[image]http://tingle.fm/wiki/assets/images/default-thumb.gif[/image]
gif[/image]
[line color=#D4D4D4][/line]
[/border]
The code I use is this:
<?php
$query = mysql_query("SELECT FROM wiki WHERE id='1'");
$assoc = mysql_fetch_assoc($query);
echo nl2br($assoc['content']);
?>
Is there any way I can output the text as raw please so it indents? Much appreciated!
If this is the output on your page, then make sure you can see those spaces/tabs also in the source code. If you can, then you can use str_replace to replace those white characters with something that will be also visible on the page
// Replacing white space characters with HTML entities
$replacedSpaces = str_replace("\n ", "\n ", $assoc['content']);
// Replacing new line characters with <br> and printing it out
echo nl2br($replacedSpaces);

I can't get asXml to return like it should

I have some straightforward code, which should return an xml string, but when i run this it returns only:'132963660013292910001330196400' which is all three of the second chile of all three concerts concatenated together. This is literally copied straight out of a textbook so i don't see how i could be doing it wrong to get this result. What am I misunderstanding here?
$simplexml = new SimpleXMLElement(
'<?xml version="1.0"?><concerts />');
$concert1 = $simplexml->addChild('concert');
$concert1->addChild("title", "The Magic Flute");
$concert1->addChild("time", 1329636600);
$concert2 = $simplexml->addChild('concert');
$concert2->addChild("title", "Vivaldi Four Seasons");
$concert2->addChild("time", 1329291000);
$concert3 = $simplexml->addChild('concert');
$concert3->addChild("title", "Mozart's Requiem");
$concert3->addChild("time", 1330196400);
echo $simplexml->asXML();
/* SHOULD output:
<concerts><concert><title>The Magic Flute</title><time>1329636600➥
</time></concert><concert><title>Vivaldi Four Seasons</title>➥
<time>1329291000</time></concert><concert><title>Mozart's Requiem➥
</title><time>1330196400</time></concert></concerts>
*/
Sounds like you're viewing the output of your script via a browser and it's attempting to interpret the document as HTML (the default content-type for PHP scripts).
Add this anywhere before your last line (the echo line)
header('Content-type: text/xml');
Alternatively, if you would like to see this as an HTML document, try this one
echo '<pre>', htmlspecialchars($simplexml->asXML()), '</pre>';
Demo here - http://codepad.viper-7.com/BJ2adH

PHP json_encode html element contains unwanted white space

I have a problem here.
index.php
ob_start();
include '../view/user.php';
$include = ob_get_clean();
echo json_encode(array(
"success" => true,
"status" => "ok",
"data" => $include));
user.php
<div>
<h2 class='workspace-name'>
<?php echo $name; ?>
</h2>
</div>
The problem is if I indent the HTML element in user.php properly (for readability), there will be a lot of \r\n\t\t\t, provided I use jquery.get to get JSON dataType.
How do I get rid of the /r/t/n? Although it doesn't display on screen I don't feel right about it. Is there any better solution?
Any question please drop in the comment I will edit this. thanks
Why not use str_replace() to replace those characters.
"data" => str_replace(array("\n","\r","\t"),'',$include)));
EDIT: Or use the following when dealing with HTML like <a\thref='#'>Click\n\nHere</a> (thanks to #Salman A for pointing this out)
"data" => str_replace(array("\n","\r","\t"),' ',$include)));
This is so ugly, but it is how I do it:
$html = str_replace("\t",' ',$html);
$html = str_replace("\r\n",'<br />',$html);
I'll be following this for a better answer. There must be a regex way.
$include = preg_replace("#[\\r|\\n|\\t]+#", "", ob_get_clean());

PHP - Display Tags Within Tag as Text

Sorry for not being able to make the title clearer.
Basically I can type text onto my page, where all HTML-TAGS are stripped, except from a couple which I've allowed.
What I want though is to be able to type all the tags I want, to be displayed as plain text, but only if they're within 'code' tags. I'm aware I'll probably use htmlentities, but how can I do it to only affect tags within the 'code' tag?
Can it be done?
Thanks in advance guys.
For example I have $_POST['content'] which is what's shown on the web page. And is the variable with all the output I'm having problems with.
Say I post a paragraph of text, it will be echoed out with all tags stripped except for a few, including the 'code' tag.
Within the code tag I put code, such as HTML information, but this should be displayed as text. How can I escape the HTML tags to be displayed as plain text within the 'code' tag only?
Below is an example of what I may type:
Hi there, this is some text and this is a picture <img ... />.
Below I will show you the code how to do this image:
<code>
<img src="" />
</code>
Everything within the tags should be displayed as plain text so that they won't get removed from PHP's strip_tags, but only html tags within the tags.
If it's STRICTLY code tags, then it can be done quite easily.
First, explode your string by any occurences of '' or ''.
For example, the string:
Hello <code> World </code>
Should become a 4-item array: {Hello,,World!,}
Now loop through the array starting at 0 and incrementing by 4. Each element you hit, run your current script on (to remove all but the allowed tags).
Now loop through the array starting at 2 and incrementing by 4. Each element you hit, just run htmlspecialentities on it.
Implode your array, and now you have a string where anything inside the tags is completely sanitized and anything outside the tags is partially sanitized.
This is the solution I found which works perfectly for me.
Thanks everyone for their help!
function code_entities($matches) {
return str_replace($matches[1],htmlentities($matches[1]),$matches[0]);
}
$content = preg_replace_callback('/<code.*?>(.*?)<\/code>/imsu',code_entities, $_POST['content']);
Here is some sample code that should do the trick:
$parsethis = '';
$parsethis .= "Hi there, this is some text and this is a picture <img src='http://www.google.no/images/srpr/logo3w.png' />\n";
$parsethis .= "Below I will show you the code how to do this image:\n";
$parsethis .= "\n";
$parsethis .= "<code>\n";
$parsethis .= " <img src='http://www.google.no/images/srpr/logo3w.png' />\n";
$parsethis .= "</code>\n";
$pattern = '#(<code[^>]*>(.*?)</code>)#si';
$finalstring = preg_replace_callback($pattern, "handle_code_tag", $parsethis);
echo $finalstring;
function handle_code_tag($matches) {
$ret = '<pre>';
$ret .= str_replace(array('<', '>'), array('<', '>'), $matches[2]);
$ret .= '</pre>';
return $ret;
}
What it does:
First using preg_replace_callback I match all code inside <code></code sending it to my callback function handle_code_tagwhich escapes all less-than and greater-than tags inside the content. The matches array wil contain full matched string in 1 and the match for (.*?) in [2].#si` s means match . across linebrakes and i means caseinsensitive
The rendered output looks like this in my browser:

Categories