PHP new line character not working [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to print a new line character in browser but '\n' in echo statement does nothing. Whats wrong in this code?
$n = 5;
for($i= 0; $i<=$n; $i++)
{
for($j=0; $j <= $n-$i-1; $j++)
echo " ";
for( ; $j <= $n; $j++)
echo '* ';
echo "\n";
}

It's actually working, if you see your html source you will find it, whereas HTML needs <br> instead of new line character. if you want to convert new line to <br> you can use php function nl2br.
echo nl2br($stingHasNewLineChar);

The "echo" command in PHP sends the output to the browser as raw html so even if in double quotes the browser will not parse it into two lines because a newline character in HTML means nothing. That's why you need to either use:
echo [output text]."<br>";
when using "echo", or instead use fwrite...
fwrite([output text]."\n");
This will output HTML newline in place of "\n".

Related

PHP string larger than 2048 charatcter turns tu null [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
In contradiction with the documentation, in the following cycle (I am creating a select) when the variable $retval reaches 2048 characters, it's set to null.
foreach ($cache_ecmcategories as $category) {
$retval.= '<option value="'.$category['rowid'].'"';
if (($isfilter ? $category['label'] : $category['rowid']) == $defaulttx)
$retval.= ' selected="selected"';
$retval.= '>'.$category['label'].'</option>'; <== This line generate the problem
}
Also, change the PHP version from 5.6.25 to 7.0.10, the problem remains the same
What is the cause of this strange behavior?
I can't reproduce your issue on my server with the following script:
<?php
$foo='';
for($i=0; $i < 1000; $i++) $foo .= "cur:$i ";
var_dump($foo);
Even an additional $foo .= NULL; doesn't trigger the bug for me.
Are you really sure that the bug happens on your posted code? Have you tried to add additional debug statements?

Php string concatenating variables with forward slash [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am trying to create a unique id by concatenating 3 variables using forward slashes(/). my code is like so
$year . "/" . $acronym . "/" . $num;
I am expecting an output of
"18/MC/1"
but the output I get is
"18\/MC\/1"
What am I doing wrong. I have already tried using stripslashes() but it doesn't do anything to the output.
You could use join function
<?php
// ...
join([$year, $acronym, $num], '/');
For more info, see: Join function documentation
I found what was wrong,
The code below outputs the correct format 18/MC/1
return response()->json([$id]);
while my previous code was
return response()->json($id);
which gave me an output of
`"18/MC/1"
There is function in php to remove backward slash from string use following function.
echo stripslashes(string);
output: 18/MC/1

Why does $n = $n + $n not work [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I've tried out some code where I use a for loop in PHP to add up numbers but it seems to not work, how can be?
$n = 3; //or anything else
for($i=0;$i<$n;$i++){
$n = $n+$n;
}
echo $n;
This is just a test code for something else, but I'd still like it to work, please help
You are increasing the loop ending condition $n inside the loop
this means it will never exit the loop.
this is an infinite loop, and wont ever print anything.
also your loop needs to be $i = 0; $i < $n; $i++

find and replace special characters in PHP [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Please tell me how to do a find and replace in PHP.
I nee to find Entry="OK" and replace with OK in a xml file.
I tried str_replace but confused because of the = and " in it.
Even though I'm not sure of the environment you're working in, here's a php sample that I think does what you want:
<html>
<?php
$teststr = "Entry=\"OK\"";
print $teststr . "<br>";
$newanswer = str_replace($teststr, "Entry=OK", $teststr);
print $newanswer . "<br>";
?>
</html>
Try this way
str_replace('Entry="OK"','OK',$var);
You can use str_replace its working
$r = 'Entry="OK" tyyuu hhh';
echo str_replace($r,'Entry="OK"','OK');
if any error found then u use preg_replace with corresponding patteren

PHPWord bold certain words on a line [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I was wondering if there was a way to bold certain words on a line. For example if I wanted every third word on a line bold, how would I do it. I am currently using addText but that requires the whole line to be bold or not bold. Any responses would be greatly appreciated.
You will need to use createTextRun() method. I have tried with Text.php file from Examples folder, and here is code relevant for your problem:
$textrun = $section->createTextRun();
$sentence='I am sentence, and every third word will be bold. This is bold.';
$word_arr=explode(' ', $sentence);
$styleFont = array('bold'=>true, 'size'=>16, 'name'=>'Calibri');
$styleFont2 = array('bold'=>false, 'size'=>16, 'name'=>'Calibri');
$c = 0;
for($i = 0; $i < count($word_arr); $i++)
{
$c++;
if($c % 3 == 0)
{
$textrun->addText($word_arr[$i].' ', $styleFont);
}
else
{
$textrun->addText($word_arr[$i].' ', $styleFont2);
}
}
You could tweak it to get what you want, but, basically, by using of mentioned method, it is possible to get different styles in same line.

Categories