Newlines not being interpreted when passed to php via the command line
I have a PHP script that I'm invoking from another shell script that sends an automated email with a message generated from the shell script. Problem is, when I send the message all the newline characters are printed into the message. How do I get them to be interpreted?
sendmail.sh:
/path/to/phpscript/sendmail.php "Some Message With Newlines\nHello World.\n"
sendmail.php:
$message = $argv[1] . "\nNewline";
$smtp->send($to, $from, $message);
The odd thing is the \n after the $argv variable is interpre开发者_StackOverflow社区ted and actually prints Newline on a new line, but the newlines in the $argv variable don't, I have tried wrapping the variable in double quotes among other things but so far to no avail.
What about calling your script with real newlines :
$ php temp.php "Some Message With Newlines
> Hello World.
> "
With temp.php
containing this :
var_dump($argv[1]);
Gets me the following output :
string(40) "Some Message With Newlines
Hello World.
"
Edit : another solution could be to use something like this to call your PHP script :
$ echo -e "Some Message With Newlines\nHello World.\n" | php temp.php
And, modify your PHP script so it read from stdin
, instead of $argv
:
var_dump(file_get_contents('php://stdin'));
精彩评论