Shawn
Shawn

Reputation: 34229

php passing strings to external diff command

My program generates two strings and I want them compared by the external diff tool. The diff tool accepts only files/directories as arguments. That's diff file1 file2 works perfectly but diff "hello" "world" doesn't work. Is there a way to pass my strings directly to diff without creating any temporary files? Thanks.

Upvotes: 0

Views: 221

Answers (1)

iblue
iblue

Reputation: 30424

On the shell, you can use temporary pipes.

diff <(echo "string 1") <(echo "string 2")

Use the backticks operator or any other method to execute the command in php. For details on executing commands, see the manual: http://www.php.net/manual/en/ref.exec.php

Make sure, you properly escape the strings.

EDIT: This feature is called temporary pipes. So the shell translates it to a file descriptor.

iblue@nerdpol:~$ echo <(echo "string")
/dev/fd/63
iblue@nerdpol:~$ cat <(echo "string")
string

For a detailed explanation see http://www.linuxjournal.com/article/2156?page=0,1

Upvotes: 2

Related Questions