Michael
Michael

Reputation: 309

deleting and replacing string inside .php file

I am having some problems with loading a php file and then replacing his content with something else. my code looks like this

$pattern="*random text*"
$rep=" "
$where=`ls *.php`
find -f $where -name "*.php" -exec sed -i 's/$pattern/$rep/g' {} \;

This wont load entire line of text. Also is there a limit of how many character can $pattern load? Also is there a way to make this .sh file execute on every 15min for example?

i am using mac osX.

Thanks!

Upvotes: 0

Views: 87

Answers (1)

fedorqui
fedorqui

Reputation: 289795

The syntax $var="value" is wrong. You need to say var="value".

If you just want to do something on files matching *.php, you are doing it in just a directory, so there is no need to use find. Just use for loop:

pattern="*random text*"
rep=" "
for file in *.php
do
   sed -i "s/$pattern/$rep/g" "$file"
done

See the usage of sed "s/$var/.../g" instead of sed 's/$var/.../g'. The double quotes expand the variables within the expression; otherwise, you would be looking for a literal $var.

Note that sed -i alone does not work in OS X, so you probably have to say sed -i ''.


Example of replacement:

Given a file:

$ cat a
hello
<?php eval(1234567890) regular php code ?>
bye

Let's remove everything from within eval():

$ sed -r 's/(eval\()[^)]*/\1X/' a
hello
<?php eval(X) regular php code ?>
bye

Upvotes: 1

Related Questions