How to Replace Character into a File with PHP

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
blackrain
Forum Newbie
Posts: 2
Joined: Fri May 19, 2006 9:24 am
Location: Amiens, France

How to Replace Character into a File with PHP

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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...
blackrain
Forum Newbie
Posts: 2
Joined: Fri May 19, 2006 9:24 am
Location: Amiens, France

Perl Script

Post 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:
Post Reply