Page 1 of 1

How to Replace Character into a File with PHP

Posted: Fri May 19, 2006 9:35 am
by blackrain
Hi! I'm working on Linux station. I would like to replace variable value into a Perl Script. I know this shell command:
perl -pi -e \'s/FirstWordToChange/ByThisOne/g\' FileName
And I've tried to launch it in my PHP script like this:
system("perl -pi -e \'s/FirstWordToChange/ByThisOne/g\' FileName");
Or
exec("perl -pi -e \'s/FirstWordToChange/ByThisOne/g\' FileName");

the variables I must replace the values are in a Perl Script which I Run later :
exec(perl FileName >file.log); //this one works very good

how can I procede?

Posted: Fri May 19, 2006 11:51 am
by Chris Corbyn
:? Sorry I'm not following you. I'd probably need to see the perl code you're referring to and the PHP code that needs to use it.

In essence PHP won't communicate with perl... you'll need to have the perl script output something to SDOUT so that PHP can read it and parse it. This probably works the same the other way, unless you pass the variables as command line arguments and read them from $argv.

Posted: Fri May 19, 2006 9:02 pm
by timvw
I usually use backticks to execute a shell command...

Code: Select all

<?php
`/usr/bin/perl -e 'my perl code'`;
?>
I wonder why you need a 'temporary' file.. Simply use the 'modified program' as the input for the second call to perl -e...

Perl Script

Posted: Mon May 22, 2006 3:58 am
by blackrain
ok ok. thanks to you two. I think I'm going to change the perl code. I'll use arguments and I'll run just one perl script:
<?php
`/usr/bin/perl -e'My_perl_code $arg1 $arg2 ......'
?>

:wink: