简体   繁体   中英

How to pass CRLF as an argument to command line program?

I need to pass an actual CRLF to a program as an argument.

If I do:

shell_exec('echo "Hello World" >t.txt');

it works fine. But when I do:

shell_exec('echo "Hello 
World" >t.txt');

it breaks. I need a way to pass an actual CRLF as an argument because ffmpeg requires it when using -headers.

I need a solution for Windows/Linux.

shell_exec('echo "Hello \n World" >t.txt');

$ cat t.txt
Hello
 World

Try using PHP_EOL instead of the escaped newline and/or carriage return.

<?php
shell_exec('echo "Hello' . PHP_EOL . 'World" >t.txt');
?>

This worked for me on Linux and OS X — I don't have a Win system to try it on.

Also note it may be CMD.EXE implementation of echo that doesn't like the newline -- other programs may deal with it as you expect, so it's worth trying on ffmpeg .

( edit: as Tom notes, windows PowerShell doesn't have the problem that cmd.exe does)

If all you're doing is creating t.txt to use as input to ffmpeg you can create it directly from PHP instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM