Multiline block showing as one line PHP - php

I'm just running some example PHP code verbatim, but it's outputting as a single line in my browser. I'm expecting to see new multiple lines.
<?php
$author = "Alfred E Newman";
echo <<<_END
This is a Headline
This is the first line.
This is the second.
- Written by $author.
_END;
?>

Your browser by default assumes that any output is HTML and when displaying HTML, newline characters are treated like spaces. You'd either need to output HTML with BR or P tags to force newlines or you can send a content-type header to tell the browser that the output you are sending is plain text.
<?php
$author = "Alfred E Newman";
// tell the browser that your output is plain text
header("Content-Type: text/plain");
echo <<<_END
This is a Headline
This is the first line.
This is the second.
- Written by $author.
_END;
?>

<?php
$author = "Alfred E Newman";
$str = "This is a Headline
This is the first line.
This is the second.
- Written by $author.
";
echo nl2br($str);
?>
will give you what you need;

Related

using fpdf : str_pad doesn't work with spaces, it works with other characters

I am adding a pad to my string, to fill with spaces, but it doesn't work
the code is here
<?php
$string1 = "Product 1 ";
$newString = str_pad($string1,100);
echo $newString."test";
echo "<br>";
$string2 = "Product 2222 ";
echo str_pad($string2,100," ")."test";
echo "<br>";
?>
the output is like this:
Product 1 test
Product 2222 test
You could try $str = str_pad($string2,(100*strlen(" "))," ")."test"; instead.
renders to a non-breaking-space in html (and when writing to document with fpdf).
Please note this can only work with fpdf when you tell it to write all lines as html! And the encoding should be utf-8 probably
$fpdf->Write(iconv('UTF-8', 'windows-1252', html_entity_decode($str)));
When the output of the PHP is converted to HTML, all the white spaces except the first are removed and it is the default feature of HTML and web browsers. so the output will not be correct.
You have to use the " " instead of white space in the str_pad function. HTML don't ignore the " " and against each existance of it, HTML adds a white space to the string.

text-indent on user formatted text

I would like to use the text-indent property (or something like this) to add a indentation of the first line of each paragraph.
First the user can write his text in a textarea, then save it in a DB.
When I want to display this text i use :
$exhib = $res->fetch_array();
echo "<div class='infoContent'>". nl2br($exhib['description']) . "</p></div>";
The line return of the user are stored as \n in DB, and modified to <br /> by nl2br. With my CSS :
.infoContent
{
text-indent: 10px;
}
only the first line is indented. (normal behavior).
Q : How can I make this indentation automatic for each line after a <br /> tag ?
I tried a ugly solution, but it doesn't work because empty paragraph section <p></p> doesn't create another line return (in case the user enter 2 line return \n\n).
echo "<div class='infoContent'><p>" . str_replace("<br />", "</p><p>", nl2br($exhib['description'])) . "</p></div>";
I can replace <p></p> tag by <br /> but it seems to be a very bad solution...
EDIT:
JSfiddle
Thanks
\n\n usually means a new paragraph (enter). The white space between paragraphs is CSS and is actually default browser styling (1em I think?). \n is a <br> (shift + enter).
So don't use nl2br() and do it yourself:
$text = '<p>' . htmlspecialchars($text) . '</p>'; // HTML ENCODE!
$text = preg_replace('#\n\n\n*#', '</p><p>', $text); // 2 or more \n
$text = preg_replace('#\n#', '<br />', $text); // all left-over \n
$text = preg_replace('#><#', ">\n<", $text); // if you like </p>\n<p> with a newline between, like I do
http://3v4l.org/b0AhL
This is pretty much what Markdown does (and Textile and those): 1 newline = BR (not exactly in Markdown) and 2 newlines = P. I always use simple Markdown for rendering plain text.
When you submit your textarea, instead of using CSS to indent only the first line, you can use (non-breaking space).
when you submit your text area, I assume you grab it as such:
$userText = $_POST['description']
Well, before you submit to your database, you could use a simple replace - After you grab the text:
$userText = str_replace("\n", "\n ", $userText);
Then submit that to the database. When it comes back, the nl2br will still make the \n into a <br /> and then it won't see the , though the HTML will see them as four spaces (equal to an indent).
It's dirty, but simple!
Reference: http://www.w3schools.com/php/func_string_str_replace.asp

php echo html tag so it is viewable (not interpreted as code)

I am currently trying to echo a text value from a variable which contains html-style tags. <...>
$string = "variable_name";
$tag_str = "<".$string.">";
echo $tag_str;
currently this echo's as nothing as it believes it is html code. How would I go about echoing <variable_name> to the page so it is viewable and not interpreted as code by the browser?
You'll have to html encode your output
$string = "variable_name";
$tag_str = "<".$string.">";
echo htmlspecialchars($tag_str);
The angle brackets (<>) are precisely what tells the browser that it should be treated as HTML code. Instead, output the HTML-encoded versions of those otherwise special characters:
$tag_str = "<".$string.">";
Alternatively, automate this process:
$tag_str = htmlspecialchars("<".$string.">");
Use highlight_string().See below code
$string = "variable_name";
$tag_str = "<".$string.">";
highlight_string($tag_str);

php Newlines vs html break

This code:
<?php
echo "This is the first line. \n";
echo "This is the second line.";
?>
Echoes out all on the same line: ("This is the first line. This is the second line.")
Shouldn't \n have basically the same function as <br> ? What am I missing here?
HTML doesn't render \n, a \n will put a break in your HTML source code
PHP
<?php
echo "<p>This is the first line. \n";
echo "This is the second line.</p>";
?>
HTML Source
<p> This is the first line.
This is the second line.</p>
HTML
This is the first line. This is the second line.
PHP
<?php
echo "<p>This is the first line.";
echo "This is the second line.</p>";
?>
HTML Source
<p> This is the first line.This is the second line.</p>
HTML
This is the first line.This is the second line.
PHP
<?php
echo "<p>This is the first line. <br/>";
echo "This is the second line.</p>";
?>
HTML Source
<p> This is the first line.<br/>This is the second line.</p>
HTML
This is the first line.
This is the second line.
You mix up the output of php and the result in your browser. \n is newline. You may see it, when you read the source code in your browser(Ctrl + U in Chrome) .
But browser only render <br> as newline on webpage
echo "This is the first line. \n";
Will produce a linebreak in your source, meaning the cursor is currently on the next line (the line belove the text).
echo "This is the second line.";
Will produce a single line and leave the cursor right after the text (on the same line).
echo "This is the second line.<br />";
Will produce a single line but in rendered html containing a visible linebreak. However, in the sourcecode there will be no linebreaks, so:
echo "Line one<br />Line two";
Will render two lines in html but one line in the source.
echo "This is the first line.", '<br />', PHP_EOL;
^ HTML code for BR and ENTER.ENTER is visible in the source or PRE tags, or TEXTAREAs, or when enabled by CSS white-space (etc.) while BR is the line break in HTML.

Php line output

I have a file named file.txt that looks like this:
Line 1
Line 2
Line 3
Line 4
using the command:
$content = file_get_contents(file.txt);
echo $content
I get the output on one line:
Line 1 Line 2 Line 3 Line 4
How can I get the output printed over 4 lines?
You actually print it to 4 lines but you can't see in it your browser because of parsing as html.
Use nl2br() to add <br/>'s
$content = file_get_contents(file.txt);
echo nl2br($content);
Also you may send headers that will say that it is not html:
header('Content-type: text/plain');
$content = file_get_contents(file.txt);
echo $content;
<?php echo nl2br($content); ?>
Replaces all regular line breaks ("\n") with "< br >" tags.
If you just want to show a plain text file you should change the content type to text/plain
<?php header("Content-type: text/plain"); ?>
Then all linebreaks will be there as they should in a document.

Categories