Why Is my line break not working?
for($n=1; $n<=100; $n++)
{
echo $n '\n';
}
?>
You have syntax error there, it should be $n . '\n'
You are using ' single quote to quote the new line (\n), thus it's being interpreted as literal \ and n, change your code to: $n . "\n" to make it outputs as newline
Final code:
for($n = 1; $n <= 100; $n++)
{
echo $n . "\n"; // or "$n\n" (thanks #ring0 for pointing that out)
}
new lines are ignored in HTML. Use <br /> instead:
for($n=1; $n<=100; $n++)
{
echo $n . '<br />';
}
Use the dot (.) operator for string concatenation.
echo $n . "\n";
It needs to be in doule quotes:
Echo $n . "\n";
First thing your concatination is wrong. It should be
echo $n ."\n";
Next thing, if you are using it to output in browser, you should use <br />
echo $n."<br />";
If you are writing it to files or console and you want to be platform independent, use PHP_EOL
echo $n.PHP_EOL
Well, also remember that Newline characters are totally ignored in HTML (otherwise you'd have to do markup all on one line!)
If you're looking to get that effect, I'd recommend wrapping your output in nl2br, which converts your newlines into HTML breaks "" so that they display properly.
nl2br($n . "\n");
or just
echo $n . "<br>";
Related
So I get these values from a form and they are then saved into a word document.
If my input (this is a textarea by the way) reads this:
"This"
&
"That"
I would expect the output to be exactly like that
However, whenever it comes out it looks like this:
It adds those special block characters at the end...
How can I get rid of these?
These are my variables:
$multipleImports = explode("\n",$_POST['multipleImports']);
$multipleImportsInfo = explode("\n",$_POST['multipleImportsInfo']);
$multipleImportsCounts = explode("\n",$_POST['multipleImportsCounts']);
And here I concatenate them into a string.
$length = count($multipleImports);
for ($i = 0; $i < $length; $i++) {
$content = $content . $multipleImports[$i] . " " . $multipleImportsInfo[$i] . " " . $multipleImportsCounts[$i] . "\n ";
}
I tried to right trim, I tried to use html entities and html decode entities and nothing I tried worked. Please help.
After reading #Tom Hedden's post it gave me an idea to try this, and it worked!
$length = count($multipleImports);
for ($i = 0; $i < $length; $i++) {
$content = $content . $multipleImports[$i] . " " . $multipleImportsInfo[$i] . " " . $multipleImportsCounts[$i] . "\r\n ";
}
I would be curious to know what those special characters are. You should do a hex dump to see. I just glanced at your code and haven't thought seriously about it, but what immediately pops into my mind is the different in end-of-line in Windows vs. *nix. That is, if the data comes from Windows I think the end of line is provided by a carriage return AND line feed ("\r\n") rather than by just a line feed ("\n").
My question is exactly opposite to this one i added last night .
Need to remove the last br tag.
Input:
Test1 is here<br><br>Now comes Test2<br><br>Then test 3<br><br><br>Thats it.
Output
Test1 is here<br>Now comes Test2<br>Then test 3<br><br>Thats it.
My try:
preg_replace("[((?:<br>)+)]","",$posttext)
It removes all breaks.
You can substitute
<br><br>(?!<br)
to <br>
preg_replace('/<br><br>(?!<br)/', "<br>", $posttext);
The lookahead will prevent to match any more <br>
See demo at regex101
Feast your eyes on this hahaha
If Preg replace doesn't work...
// cuts off one <br> as high as whatever $i is at
$string = "Test1 is here<br><br>Now comes Test2<br><br>Then test 3<br><br><br>Thats it.";
$i = 10;
while($i > 0)
{
$ii = 1;
$brake = "<br>";
$replace = "";
while($ii < $i)
{
$brake .= "<br>";
$replace .= "<br>";
$ii++;
}
$string = str_replace($brake,$replace,$string);
$i--;
}
echo $string; // Test1 is here<br>Now comes Test2<br>Then test 3<br><br>Thats it.
PS: If theres no preg replace for this, it is usable albeit very inefficent.
I have the following line in my code which displays my output in 6 characters with leading zeros.
$formatted_value = sprintf("%06d", $phpPartHrsMls);
I want to replace the leading zeros with spaces. Have tried all the examples found by searching this site and others and cannot figure it out.
Here are some I have tried:
$formatted_value = sprintf("%6s", $phpPartHrsMls);
$formatted_value = printf("[%6s]\n", $phpPartHrsMls); // right-justification with spaces
In the browser, spaces will always be collapsed.
Try:
<pre><?php echo $formatted_value; ?></pre>
And once you're satisfied with that, take a look at the CSS white-space:pre-wrap - a very useful property!
This will align the left with spaces:
$formatted_value = sprintf("%6s", $phpPartHrsMls);
echo "<pre>" . $formatted_value . "</pre>";
This will align the right with spaces:
$formatted_value = sprintf("%-6s", $phpPartHrsMls);
echo "<pre>" . $formatted_value . "</pre>";
If you want to print only six digits, and others to remove:
$formatted_value = sprintf("%-6.6s", $phpPartHrsMls);
echo "<pre>" . $formatted_value . "</pre>";
One more thing, the browser will generally ignore the spaces, so it's better to wrap your output in <pre> tag.
Changing leading zeroes to leading spaces:
$formatted_value = sprintf("%' 6s", $phpPartHrsMls);
Try str_pad.
str_pad($phpPartHrsMls, 6, " ", STR_PAD_LEFT);
you use %06d please try some larger number .
your code can some thing like below try :
printf('%20.2f', $phpPartHrsMls);
and you can use for space on your html .
Probably a simple problem here, but I cannot find it.
I am exploding a string that was input and stored from a textarea. I use nl2br() so that I can explode the string by the <br /> tag.
The string explodes properly, but when I try to get the first character of the string in a while loop, it only returns on the first line.
Note: The concept here is greentexting, so if you are familiar with that then you will see what I am trying to do. If you are not, I put a brief description below the code sample.
Code:
while($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$comment = nl2br($row['comment']);
$sepcomment = explode("<br />", $comment);
$countcomment = count($sepcomment);
$i = 0;
//BEGIN GREENTEXT COLORING LOOP
while($i < $countcomment) {
$fb = $sepcomment[$i];
$z = $fb[0]; // Check to see if first character is >
if ($z == ">") {
$tcolor = "#789922";
}
else {
$tcolor = "#000000";
}
echo '<font color="' . $tcolor . '">' . $sepcomment[$i] . '</font><br>';
$i++;
}
//END GREENTEXT COLORING LOOP
}
Greentext: If the first character of the line is '>' then the color of that entire line becomes green. If not, then the color is black.
Picture:
What I have tried:
strip_tags() - Thinking that possibly the tags were acting as the first characters.
$fb = preg_replace("/(<br\s*\/?>\s*)+/", "", $sepcomment[$i]);
str_replace()
echo $z //Shows the correct character on first line, blank on following lines.
$z = substr($fb, 0, 1);
Here is a test I just did where I returned the first 5 characters of the string.
Any ideas for getting rid of those empty characters?
Try "trim" function
$fb = trim($sepcomment[$i]);
http://php.net/manual/en/function.trim.php
(probably line breaks are the problem, there are \n\r characters after tag)
I am trying to print a variable between curly braces as
Product_number{product_version}
I tried
echo "$product_number{$product_version}";
But that does not work. I don't understand why :(
try using double braces:
echo "$product_number{{$product_version}}";
You can also do:
echo "$product_number{".$product_version."}";
{ followed by $ is treated specially. It is mainly used when you want to append a string immediately at the end of a variable's value:
$v = 'hack';
echo "I {$v}ed it";
echo $product_number . "{" . $product_version . "}";
Escape the "{":
echo "$product_number\{$product_version}";