This is a little silly, but something I've wanted to do before and could never figure out. I have a PHP script that runs from the command line. I'd like a little timer animation to let the user know the script is still running. Here's what I have:
while (1 == 1) {
echo '—';
usleep(100000);
echo '\';
usleep(100000);
echo '|';
usleep(100000);
echo '/';
}
But how do I get each echo to replace the character before it?
You need to print a backspace before each character, for example:-
echo '-';
usleep(100000);
echo "\b/";
Note that you have to use double quotes here or the escape sequence won't work.
I'm sure you can work the rest out :)
If "\b" doesn't work try:-
echo chr(8) . '/';
Try the php ncurses extension:
http://php.net/manual/en/ref.ncurses.php
You can use
echo "yourCharacter1\r";
usleep(100000);
echo "yourCharacter1\r";
or
echo "\ryourCharacter1\r";
usleep(100000);
echo "\ryourCharacter2\r";
\r sends the cursor back to position 0 on the same line.
Also you may try (found online)
system("clear"); // before you echo new characters
or
passthru('clear'); // before you echo new characters
Related
I'm using PHP to send a string with $_GET to another webpage.
My main issue is when I try to see the whole string in my link just appear the first word "Im".
I'm pretty sure it is by double quotes but I'm not sure to do this.
<a href="edit.php?title=library&description=Im" going="" to="" the="" library="" tomorrow="">
I try to put this in my PHP code to replace "" character to '' but it doesn't work.
$length = strlen($_GET['description']);
$i=0;
do{
$_GET['description'][$i] = preg_replace('/["]/', '', $_GET['description][$i]);
$i++;
}while($i < $length);
My PHP link is the next:
echo "<a href=edit.php?title=$_GET[title]&description=$_GET[description]>";
If anyone knows what's going on, please help me.
Have you tried using the rawurlencode function?
// if $_GET["title"] = "Im going to the library tomorrow" ...
echo "<a href=edit.php?title=" . rawurlencode($_GET["title"]) . "&description=" . rawurlencode($_GET["description"]) . ">";
You may need to use rawurldecode when you receive this.
I wrote a short test code in PHP7:
<?php
$str1=' bigapple ';
echo strlen($str1);
trim($str1) ;//or trim($str1," ")
echo strlen($str1);
?>
But whenever I use trim on $str1 ,the strlen would be return 10.
Can someone tell me the reason why? I've been searching it but find nothing.
You need to store trim string to any variable or you need to print trim string as following:
echo strlen(trim($str1));
You have not assigned the trimmed value in another variable. Try as below :
<?php
$str1=' bigapple ';
echo strlen($str1).'<br>';
$str2 = trim($str1);
echo strlen($str2).'<br>';
?>
I want to echo string dynamically, not all of them at once when script finished running. Tried this one, but it echos all of them when script finished running. How can I echo values dynamically ?
<?php
ob_start();
echo "Line #1...<br>";
ob_flush();
flush();
sleep(2);
echo "Line #2...<br>";
ob_flush();
flush();
sleep(2);
echo "Line #4...<br>";
?>
Try sending a line-ending like \n or append at least 256 spaces to each echo to trigger the browser.
Some browsers will wait for at least 256bytes before rendering, others need a newline char. Try this combination before each flush:
echo str_repeat(" ", 256) . "\n";
Other cause could be the webserver that is caching the response.
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;
How would I do to print a ">" before every line?
if ($quoteid) {
echo '
> '.$quote['message'].'
';
}
Currently It Looks like this:
> I would move Heaven and Hell and anything in between to get to you.
You wouldn't be safe anywhere if I was mad at you.
And that's not bull; that's truth. I've went up against people.
You could pull a gun on me and if I'm mad at you I'm coming forward.
You'd have to shoot me to stop me and if you don't kill me... you're stupid cause the next time you see me I will kill you.
I want it to look like this:
> I would move Heaven and Hell and anything in between to get to you.
> You wouldn't be safe anywhere if I was mad at you.
> And that's not bull; that's truth. I've went up against people.
> You could pull a gun on me and if I'm mad at you I'm coming forward.
> You'd have to shoot me to stop me and if you don't kill me... you're stupid cause the next time you see me I will kill you.
if ($quoteid) {
// Replace new line with new line + "> "
echo '>' . str_replace("\n", "\n> ", $quote['message']);
}
if ($quoteid) {
echo ' > '.str_replace("\n","\n > ",$quote['message'])';
}
use str_getcsv or explode to get the lines of your textblock and then print every line (like you already do).
try with str_replace
$bodytag = str_replace("\n", ">", $quote['message']);
echo str_replace(array("\r\n", "\n"), "\n> ", $quote['message']);
try this sinp
if ($quoteid) {
echo str_replace("\n", "\n> ", $quote['message']);
}
echo '>'.substr("\n", "\n>", $quote['message']);