add a new element to an array as a line break - php

How to set an element of an array as a line break?
For example:
$array(1,2,3,'\n');
I try to set the element as '\n' but It will print exactly '\n', not a line break when imploded.
implode('',$array);

Use double quotes "\n" instead of '\n' single quotes, Single quotes treats as string
<?php
$arr = [1,2,3,"\n","after line break"];
echo implode('',$arr);
?>
Live demo : https://eval.in/865569
In your browser to add line break use <br> instead \n

Related

How to crete Bold text with a new line of normal text in the same cell

I want to create this format of cell which is Bold and normal in the same cell and different line.
This is the actual code I working on
$objRichText = new PHPExcel_RichText();
$objBold = $objRichText->createTextRun('some number');
$objBold->getFont()->setBold(true);
$objBold->getFont()->setSize(10);
$objRichText->createText('\nsome text');
$objPHPExcel->getActiveSheet()->getCell('A1')->setValue($objRichText);
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setWrapText(true)->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
But the result are still in the same line " some number\nsome text"
PHP Strings 101
\n needs to be in Double quotes (") not single quotes (') if you want it to be interpreted as a new line, and not as a literal \n
$objRichText->createText("\nsome text");

How to insert a new line break within the php code

<?php echo nl2br($valueu->getarea($rows['province'].'|'.$rows['city'].'|'.$rows['area'],' ')); ?></td>
how to put a line break in between city and area when outputted to a browser.
Thanks
Use \n. Just add it with the sting like this I."\n" like pie.
You can also use nl2br like this echo nl2br("One line.\nAnother line.");
Try double quote "\n".
If you use single quote '\n', PHP will just interpret as a literal \n, but within double quote, it will parse it as an escape sequence (newline character) and echo it. More about strings here.
echo nl2br('Hello \n world');
// -> Hello \n world
echo nl2br("Hello \n world");
// -> Hello <br /> world
In a browser, you may need to use "<br/>" instead of a newline "\n".
echo($valueu->getarea($rows['province'].'|'.$rows['city'].'<br />'.$rows['area'],' '));
Something like that?

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?

Passing string to a Javascript function does not work

I am trying to pass a string to a javascript function which opens that string in an editable text area. If the string does not contain a new line character, it is passed successfully. But when there is a new line character it fails.
My code in PHP looks like
$show_txt = sprintf("showEditTextarea('%s')", $test_string);
$output[] = '<a href="#" id="link-'.$data['test'].'" onclick="'.$show_txt.';return false;">';
And the javascript function looks like -
$output[] = '<script type="text/javascript">
var showEditTextarea = function(test_string) {
alert(test_string);
}
</script>';
The string that was successfully passed was "This is a test" and it failed for "This is a first test
This is a second test"
Javascript does not allow newline characters in strings. You need to replace them by \n before the sprintf() call.
You are getting this error because there is nothing escaping your javascript variables... json_encode is useful here. addslashes will also have to be used in the context to escape the double quotes.
$show_txt = sprintf("showEditTextarea(%s)", json_encode($test_string));
$output[] = '<a href="#" id="link-'.$data['test'].'" onclick="'.htmlspecialchars($show_txt).';return false;">';
Why don't you try replacing all spaces in the php string with \r\n before you pass it to the JavaScript function? See if that works.
If that does not work then try this:
str_replace($test, "\n", "\n");
Replacing with two \ may work as it will encapsulate.
I would avoid storing HTML or JS in PHP variables as much as possible, but if you do need to store the HTML in a PHP variable then you will need to escape the new line characters.
try
$test_string = str_replace("\n", "\\\n", $test_string);
Be sure to use double quotes in the str_replace otherwise the \n will be interpreted as literally \n instead of a new line character.
Try this code, that deletes new lines:
$show_txt = sprintf("showEditTextarea('%s')", str_replace(PHP_EOL, '', $test_string));
Or replaces with: \n.
$show_txt = sprintf("showEditTextarea('%s')", str_replace(PHP_EOL, '\n', $test_string));

PHP new line problem

simple problem baffling me...
i have a function:
function spitHTML() {
$html = '
<div>This is my title</div>\n
<div>This is a second div</div>';
return $html
}
echo $spitHTML();
Why is this actually spitting out the \n's?
Backslashes used in single quote strings do not work as escape characters (besides for the single quote itself).
$string1 = "\n"; // this is a newline
$string2 = '\n'; // this is a backslash followed by the letter n
$string3 = '\''; // this is a single quote
$string3 = "\""; // this is a double quote
So why use single quotes at all? The answer is simple: If you want to print, for example, HTML code, in which naturally there are a lot of double quotes, wrapping the string in single quotes is much more readable:
$html = '<div class="heading" style="align: center" id="content">';
This is far better than
$html = "<div class=\"heading\" style=\"align: center\" id=\"content\">";
Besides that, since PHP doesn't have to parse the single quote strings for variables and/or escaped characters, it processes these strings a bit faster.
Personally, I always use single quotes and attach newline characters from double quotes. This then looks like
$text = 'This is a standard text with non-processed $vars followed by a newline' . "\n";
But that's just a matter of taste :o)
Because you're using single quotes - change to double quotes and it will behave as you expect.
See the documentation for Single quoted strings.
Change ' to " :) (After that, all special chars and variable be noticed)
$html = "
<div>This is my title</div>\n
<div>This is a second div</div>";

Categories