Explode on new line in PHP only reads first line - php

So my input is a number of lines :
AAAAA
CCSDCSDC
jhbhvhv
I use explode in PHP to have an array, each array entry is a line:
$lines=explode("\n", $text);
so when I do this
echo $lines[0];
echo $lines[1];
echo $lines[2];
I get only the first line :
AAAAA
What goes wrong ?

<?php
$str = "AAAAA\nCCSDCSDC\njhbhvhv";
$lines=explode("\n", $str);
echo $lines[0]."<br/>";
echo $lines[1]."<br/>";
echo $lines[2]."<br/>";
?>

I'm not sure, but does it make a diffrence where the php script is running on?
I mean \n for UNIX systems and \r\n for Windows. Maybe you could try this:
$lines=explode("\r\n", $text);

Related

PHP echoing variable but adding line breaks

I have a variable a such $var:
$var = "One, Two, Three";
I can echo the variable without any problems the output is:
One, Two, Three
Is it possible, when echoing the variable to add a line break where there is a ,, so it would look like this?
One,
Two,
Three
If you echo the text to HTML, you can do the following:
echo str_replace(",", ", <br/>", $var);
If you echo the string to a console or a text file through redirection, just use the PHP_EOL constant, which represents the correct end-of-line string for the current platform ie. "\n" for Unix, "\r\n" for Windows:
echo str_replace(",", "," . PHP_EOL, $var);
You can use this:
$var = "One,\nTwo,\nThree";
\n is the line break, and makes sense if you are working through the terminal
You use \n to force a new line when outputting to a terminal.
$var = "One,\nTwo,\nThree";
You can use the HTML <br /> to output a new line on a web browser.
$var = "One,<br />Two,<br />Three";
You can use the str_replace function once you determine which type you want.
Make use of <br> tag
$var = "One, <br>Two, <br>Three";
(or) Make use of str_replace in PHP
<?php
$var = "One, Two, Three";
echo str_replace(',',',<br>',$var); // code replaces all your commas with , and a <br> tag
Explode the string with the comma as separator, then iterate through the resulting array, adding line breaks (with the br tag if outputting to browser, or newline (\n) escape sequence if outputting to terminal) when needed?

Line breaks from textarea and mysql dont appear in html

How can I make text appear on diff lines in html?! They appear on diff lines in my text area when output from mysql, and also appear on diff lines inside mysql. But in the web page its all on one line. How can this be solved?
Use:
string nl2br ( string $string [, bool $is_xhtml = true ] )
It can handle \n, \r, \r\n or \n\r linebreaks and replaces them with a <br />
Example:
$yourText = "This is line one.\nThis is line two.";
$yourText = nl2br($yourText);
echo $yourText;
This will result in:
This is line one.<br />This is line two.
Link to the manual for more information.
Use the PHP nl2br function:
$string = "hello \n world";
$string = nl2br($string);
It's quite self-explanitory: \n gets replaced with <br />
Replace Linebreaks with <br>
str_replace(.N, '<br>', [element]);
You can use <pre>..Your text..</pre> tag for that.

PHP exec capture text

I am having a problem with capturing text when I make an exec call to a perl script that just prints a lot of text. What happens when I run the following code is I get a 1 word result: "Array". I need to be able to capture the results so that I can change them around just a little bit. Here is the code:
<?php
$lastline = exec("perl parseOutput.pl",$retVal);
echo $retVal;
?>
How do I work around this?
You have an array of the lines of text that was outputted.
Do something like this:
echo implode( "\n", $retVal);
Or
echo implode( "<br />\n", $retVal);
And you'll see all of the output generated by the perl script.
just use shell_exec()
$fullResult = shell_exec("perl parseOutput.pl");
echo $fullResult;

Capture new line in a string

I know for sure that this was already been asked before but I just googled around and couldn't find anything (maybe wrong word choice?).
Just don't be too mad at me, I'm getting mad too...
I'd like
$string = '
This is a line
This is another line
';
to be shown to the HTML page as
This is a line
This is another line
when I do echo $string;.
How can I capture the return key or the new line and replace it with <br>?
Try the nl2br() function:
echo nl2br($string);
Would return:
<br>
This is a line<br>
This is another line<br>
To trim off the leading and trailing new lines, use trim():
echo nl2br(trim($string));
Would return:
This is a line<br>
This is another line
You can use the PHP function nl2br. It doesn't replace the newlines but, rather, inserts <br /> next to them (which is perfectly fine for your purposes).
Using your example:
$string = '
This is a line
This is another line
';
echo nl2br($string);
/* output
<br />
This is a line<br />
This is another line<br />
*/
Use nl2br function like this:
echo nl2br($string);
If you're not getting it with nl2br, your new line character must not be \n.
print nl2br( str_replace( array( "\r\n", "\r" ), "\n", $string);
Why don't you use:
$string = "
This is a line\n
This is another line
";
?
or use
$string = <<<EOF
This is a line
This is another line
EOF;

Processing each line of a textarea form in a script

I've learned it's possible to trim a string from a textarea and put break tags after it, so each sentence written at a new line in the textbox will also be written at a new line in the PHP file.
This is the snippet:
<html>
<body>
<?php
$text = trim($_POST['textarea']);
$text = nl2br($text);
echo $text;
?>
</body>
</html>
The thing is that my true intentions are:
Use the contents of each line in the textbox for a certain script
Print the contents of each line with the results from the script added all separated by lines.
<?php
$text = trim($_POST['textarea']);
$text = explode ("\n", $text);
foreach ($text as $line) {
echo myFunction($line);
echo "< hr />";
}
?>
The PHP function explode lets you take a string and blow it up into smaller pieces. Store this array for use in other scripts
$str_arr = explode("\n", $_POST['textarea']);
//$str_arr can be used for other script

Categories