I usually use PHP to create dynamic web pages, now I need to write a script on linux that is similar to an old one I wrote for my website and I don't want to rewrite the whole thing in python.
My problem is simple, I know that '\n' is the newline character for linux, but this doesn't work:
echo 'hello world\n';
Any Ideas?
try this for linux:
echo 'hello world'."\n";
or this (works on any OS):
echo 'hello world'.PHP_EOL;
In PHP, escape characters like \n are only interpreted if the string is surrounded with double quotes, so try:
echo "hello world\n";
Related
OK so I have this line,
$ssh->exec('cd E:\\Titan\ Torque\\Jobs');
Now how to I use this with the double slashes? I mean I need 2 slashes to be sent not one the command:
cd E:\\Titan\ Torque\\Jobs
Is what I need to be executed.
P.S. The ssh server is running on windows, this command runs fine in putty but PHP is stripping it down to:
cd E:\Titan\ Torque\Jobs
Any help would be appreciated.
I think if you escape the backslashes with backslashes, they should work.
$ssh->exec('cd E:\\\\Titan\\ Torque\\\\Jobs');
In a test:
echo 'E\\:a thing with\slashes\\';
echo "\n";
echo 'E\\\\:a thing with\\\\slashes\\';
gives
E\:a thing with\slashes\
E\\:a thing with\\slashes\
In case you're curious, the reason a single backslash works at all, is because \ isn't a special escape sequence so it's put into the resultant string literally.
Well the title sums it up. If I do this:
fwrite($handle, 'test\r\ntest');
I get litreally that written to a file. That is:
test\r\ntest
It doesn't work for echo or any other function manipulates strings.
This became a problem when I needed to write to a printer in the serial port using ESC/POS. If I use PHP, it prints a bunch of question makrs and french characteres. With Python (plus pyserial), using the following code, works amazingly:
from __future__ import print_function
import serial
ser = serial.Serial('COM4');
ser.write('\x1b\x40');
ser.write('\x0a');
ser.write('\x0a');
ser.write('Hello there');
ser.write('\x0a');
ser.write('\x1d\x56\x42\x03');
My system:
WAMP 2.4 (PHP 5.4.16, Apache 2.4.4) on Windows 7 Home Basic x64
For the backslash sign to escape special characters you need to use double-quoted strings, not single-quoted ones, try these :
fwrite($handle, 'test\r\ntest'); // not working
fwrite($handle, "test\r\ntest"); // works as expected
It may be worth noting that the problem could have been skipped altogether if your data came from another source (non-php file, web form), and happens only when you hardcode your strings inside your script file. For further details, feel free to browse the relevant manual page :
http://www.php.net/manual/en/language.types.string.php
Using a single quote will literally write your string.
To use escaped characters you need to use a double quote instead :
echo "This output will \n expand";
http://www.php.net/manual/en/language.types.string.php
Try using " instead of ' for these escape sequences:
fwrite($handle, "test\r\ntest");
I am trying to write an application for Linux system user change password.
Here is the code for that:
shell_exec("/usr/bin/passwd ".$user." <<EOF\n".$password."\n".$password."\nEOF");
It take all the spacial characters accept "$"
How to pass "$" in shell_exec?
Thanks in advance.
you can escape it using \$ or include your string content inside single quote. as single quotes doesn't expand variables
shell_exec('/usr/bin/passwd $'.$user." <<EOF\n".$password."\n".$password."\nEOF");
alternatively you can also pass the command string to shellescapearg as follows
$arg = $user." <<EOF\n".$password."\n".$password."\nEOF";
shell_exec('usr/bin/passwd ' . $arg);
I do not think the error is related to the $ symbol. A code similar to yours worked just fine for me.
I used the following to test:
$msg = "Hello world"; shell_exec("notify-send ".$msg);
ok i do have this following codes
<?php
ob_start();
?>
codepad is an
online compiler/interpreter,
and a simple collaboration tool.
Paste
your code below,
and codepad wi
ll run
it and give you a short
URL you can use to share
it in chat or email
<?php
$str = str_replace('\r\n','',trim(ob_get_clean()));
echo $str;
?>
and you can see how it works here
http://codepad.org/DrOmyoY9
now what i want here is to remove the newlines from the stored output of the ob_get_clean().
I almost looked around the internet on how to remove newlines in the strings and that's the common and fastest method to remove the newlines aside from using the slowly preg_replace().
Why this happens? is this already a bug? or i just missed something?
\r\n is windows style, but if the user using linux or mac, it would be different . so best solution is:
$str = str_replace(array("\r","\n"),'',trim(ob_get_clean()));
I think u miss one thing, it should be :
$str = str_replace("\r\n",'',trim(ob_get_clean()));
using double quotes not single quotes
For some reason I can't use \n to create a linefeed when outputting to a file with PHP. It just writes "\n" to the file. I've tried using "\\n" as well, where it just writes "\n" (as expected). But I can't for the life of me figure out why adding \n to my strings isn't creating new lines. I've also tried \r\n but it just appends "\r\n" to the line in the file.
Example:
error_log('test\n', 3, 'error.log');
error_log('test2\n', 3, 'error.log');
Outputs:
test\ntest2\n
Using MAMP on OSX in case that matters (some sort of PHP config thing maybe?).
Any suggestions?
Use double quotes. "test\n" will work just fine (Or, use 'test' . PHP_EOL).
If the string is enclosed in double-quotes ("), PHP will interpret more escape sequences for special characters:
http://php.net/manual/en/language.types.string.php
\n is not meant to be seen as a new line by the end user, you must use the html <br/> element for that.
/n only affects how the html that is generated by php appears in the source code of the web page. if you go to your web page and click on 'view source' you will see php-generated html as one long line. Not pretty. That's what \n is for ; to break that php-generated html into shorter lines. The purpose of \n is to make a prettier 'view source' page.
When you run a PHP script in a browser, it will be rendered as HTML by default. If the books you’re using show otherwise, then either the code or the illustration is inaccurate. You can use “view source” to view what was sent to the browser and you’ll see that your line feeds are present.
<?php
echo "Line 1\nLine 2";
?>
This will render in your browser as:
Line 1 Line 2
If you need to send plain text to your browser, you can use something like:
<?php
header('Content-type: text/plain');
echo "Line 1\nLine 2";
?>
This will output:
Line 1
Line 2
nl2br() function use for create new line
echo nl2br("Welcome\r\n This is my HTML document", false);
The above example will output:
Welcome
This is my HTML document
I'm pretty sure you are outputting to a html file.
The problem is html ignores newlines in source which means you have to replace the newlines with <br/> if you want a newline in the resulting page display.
You need to use double quotes. Double quotes have more escape chars.
error_log("test\n", 3, 'error.log');
error_log("test2\n", 3, 'error.log');
to place the \n in double quotes try
$LOG = str_replace('\n', "\n", $LOG);
It's because you use apostrophes ('). Use quotationmarks (") instead. ' prompts PHP to use whatever is in between the apostrophes literally.
Double quotes are what you want. Single quotes ignore the \ escape. Double quotes will also evaluate variable expressions for you.
Check this page in the php manual for more.
The “\n” or “\r” or similar tags are treated as white-space in HTML and browsers. You can use the "pre" tag to solve that issue
<?php
echo "<pre>";
echo "line1 \n some text \t a tab \r some other content";
echo "</pre>";
?>
If you want to print something like this with a newline (\n) after it:
<p id = "theyateme">Did it get eaten?</p>
To print the above, you should do this:
<?php
print('<p id = "theyateme">Did it get eaten?</p>' . "\n");
?>
The client code from above would be:
<p id = "theyateme">Did it get eaten?</p>
The output from above would be:
Did it get eaten?
I know it's hard, but I always do it that way, and you almost always have to do it that way.
Sometimes you want PHP to print \n to the page instead of giving a newline, like in JavaScript code (generated by PHP).
NOTE about answer: You might be like: Why did you use print instead of echo (I like my echo). That is because I prefer print over echo and printf, because it works better in some cases (my cases usually), but it can be done fine with echo in this case.