Carriage return PHP multi line - php

I'd like to do a multi line carriage return with php for the command line.
Is this possible?
I know already how to achieve it with one line print("\r") but it like to know how to do this on multiple lines.
The printed data should look like this:
$output = "
Total time passed: 34, \n
Total tests: 14/523
";
print($output . "\r");
It works for a single line so that there is not a new line added every time it is printed in the loop.
When i use it with a PHP_EOL, or \n i'm getting new lines all the time. I just want two lines to show up while updating it.

You can do this using ANSI escape
char [F
As example:
for ($i = 1; $i < 5; $i++) {
$prevLineChar = "\e[F";
sleep(1);
echo "$i\nof 5\n" . $prevLineChar . $prevLineChar;
}

You are looking for PHP_EOL, one for each carriage return. See here: https://stackoverflow.com/a/128564/2429318 for a nice friendly description.
It is one of the Core Predefined Constants in PHP
PHP_EOL (string)
The correct 'End Of Line' symbol for this platform. Available since PHP 5.0.2

You could write something like:
print("\n\n\n\n\n");
but what if you want to print a dynamic number of new lines :)
try this function:
function new_lines($n) {
for($i = 0; $i < $n; $i++) { echo PHP_EOL; }
}
call it like so :
//print 100 new lines
new_lines(100);

Related

add numbers to new lines separated by comma [duplicate]

This question already has answers here:
PHP counter increment in a while loop
(5 answers)
Closed 2 years ago.
i am having some issues with my php code. what i wish to add numbers like before every lines something like below
1. some data
2. some more data
what i have tried doing is like below but it would not multiply the numbers like i want it to, it only prints 1
$num = 1;
$tdetails = $num++. str_replace(',', '<br />', $row['travellers_details']);
Could someone please show how to achieve what i am looking for?
thanks
You need to iterate and increment num each time. You can store the elements in an array first using explode:
$num = 1;
$tdetails = explode(',', 'some data, some more data');
$str = "";
for($i = 0; $i < count($tdetails); $i++)
$str .= $num++ . ". ". $tdetails[$i] . "<br>";
echo $str;
Output:
1. some data
2. some more data
You can make use of HTML ol(ordered list) tag which will automatically do the numbering for you.
<?php
$tdetails = "<ol><li>" . implode("</li><li>", explode(",",$row['travellers_details'])) . "</li></ol>";
echo $tdetails;

one line with multiple color text in phpword?

Is there any method to write different color of text in a same line like this but it change whole line color?
I want my line to like this:
Here is my code:
$section->addText( 'Headline: ', (array('color'=>'#70AD47')),$fontStyleIndexPara);
$section->addText(cleanstring($data[$k]['ArticleTitle']),$fontStyleIndexPara);
`
Instead of writing the individual text parts directly into the section with addText(), enclose it with addTextRun().
By using addTextRun() you can prevent the line break.
Code for your example:
$TextRun = $section->addTextRun();
$TextRun->addText( 'Headline: ', array('color'=>'70AD47'));
$TextRun->addText(cleanstring($data[$k]['ArticleTitle']));
The # is not used for the color.
Yes you can use something like that:
$sentence='Your text in this sentence the two first word will get the stylefont while the rest will get the stylefont2';
$word_arr=explode(' ', $sentence);
$phpWord->addParagraphStyle('p3Style',array('align'=>'center'));
$c = 0;
$textrun = $section->addTextRun(array('align' => 'center'));
for($i = 0; $i < count($word_arr); $i++)
{
$c++;
if($c < 2)
{
$textrun->addText($word_arr[$i].' ', $styleFont);
}
else
{
$textrun->addText($word_arr[$i].' ', $styleFont2);
}
}

Advanced Algoithm issues to generate unique 6 char long code

I have used this algorithm to generate 100 codes. Now Once again i have searched the threads on this site and on google before posting. The algorthms found do work in generating the code the issue I got is i have to generate UNIQUE codes from the one i already have. The problem i am finding is all the scripts are all failing to execute.
So basically I have an ARRAY holding 100 voucher codes I need to generate 400 more voucher codes however while my algorithm's works it keeps timing out due to it taking to long to execute. I have tried several other algorithms from the threads on Stack overflow but for some reason they all keep timing out. I need advice on what I can do to my algorithm to actually produce 400 more unique 6 Char codes.
Here is script I have
/**
**Use this portion to get my current 100 codes in an array which i use
**/
$redeem = 'THIS IS A STRING WHICH CREATES AN ARRAY CONTIANING 100 VALUES';
$redeem = preg_replace('/\s+/', '', $redeem);
$redeem_code = str_split($redeem, 6);
/**
**Function generates unique 6 letter voucher codes
**
**/
function random_str($length, $keyspace = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ')
{
$str = '';
$max = mb_strlen($keyspace, '8bit') - 1;
for ($i = 0; $i < $length; ++$i) {
$str .= $keyspace[mt_rand(0, $max)];
}
return $str;
}
/**
**This is the query which checks against my array and then echo's a valid code
**
**/
$redeem_count = count($redeem_code);
$i = 0;
$ia = 1;
while($i <= 500){
$string = random_str(6);
if(in_array($string, $redeem_code))
{
echo $string;
$i= $i+1;
}
}
Constant Error Message on All functions tried.
Fatal error: Maximum execution time of 120 seconds exceeded in C:\wamp\www\nfr_chip.php on line 115
You never add anything to the array and only increase $i if the code already exist.
This should work...
$redeem_count = count($redeem_code);
while($redeem_count <= 500) {
$string = random_str(6);
if(!in_array($string, $redeem_code))
{
$redeem_code[] = $string;
$redeem_count++;
}
}
And for the way you are splitting your string with codes, instead of using preg_replace (avoid regex unless you really need it):
$redeem_code = explode(" ", $redeem);
You can safely use openssl_random_pseudo_bytes with bin2hex to generate pseudo-random, as the name implies, strings.
Here's an example:
for ($i=1;$i<=5;$i++){
print bin2hex(openssl_random_pseudo_bytes(3)) . PHP_EOL;
}
//output
9282fb
3b9798
c187a0
a058e3
df2e4b
3 bytes will give you a 6 char string

php command line change text output

I want to know is it possible to change some output for special php cli base application to change some value on terminal not echo new one. for example this is cli application.
#!/usr/bin/env php
<?php
$percent = 0;
for ($i = 0; $i <= 100; $i++) {
echo $percent . "\n";
sleep(1);
$percent++;
}
/**
0
1
...
*/
It's a simple app to show the user the percentage. So we must update it after each loop in this example, rather than append it. I want to change percent not show new one.
use \r instead of \n. \r is a carriage return, it will jump back to the beginning of the line without a newline.
$percent = 0;
for ($i = 0; $i <= 100; $i++) {
echo $percent . "\r";
sleep(1);
$percent++;
}
This is working on Windows, Linux and MacOS
Something like this should work for what you need (tested and verified on a Linux (CentOS) machine):
for ($percent = 0; $percent <= 100; $percent++) {
echo $percent;
sleep(1);
// Print one or more backspaces, erasing current character(s)
echo str_repeat("\x08", strlen($percent));
}
This actually depends on your terminal (emulator) type, not on the language used. Have a few tries using the backspace character (0x08) to 'erase' the current content, then output the new content.

Last line and char of text file

I want to display the last chars from a textfile. The textfile is from a temperature 1-wire system. The file is sometimes big. I display the last line with this:
<?php
$file = file("file.txt");
for ($i = count($file)-1; $i < count($file); $i++) {
echo $file[$i] . "\n";
}
?>
It works great!
But how do I read the last 5 chars of that line? I want to and echo them in to a div on an html-page?
regards
Anders
Try below in for loop
echo substr($file[$i], -5) . "\n";
More Info: PHP Manual
You can use substr() passing a negative value for the start argument.
echo "last 5 chars: " . substr($file[$i], -5);
From the docs:
If start is negative, the returned string will start at the start'th character from the end of string.
Thanks for your help!
I now read, round and display the temperature with this PHP:
<?php
$file = file("file.txt");
for ($i = count($file)-1; $i < count($file); $i++) {
echo round(substr($file[$i], -5) . "\n", 1);
}
However problem when there is more or less characters.
Is there a way to read the last characters after the semicolon on the last line?
Three examples:
07.07.2012; 06:19:23;25.63 - display 25.6
09.12.2012; 06:19:23;-5.63 - display -5.6
12.09.2012; 22:58:49;-17.86 - display -17.9
regards
Anders

Categories