Jump to content

Search and delete within a file.


the_oliver

Recommended Posts

Hi,

 

Im wondering if it is possible to have a php script that will open a file, and then if a 'phrase' exists with in it, deletes the phrase, leaving the rest of the file in tact. eg:

 

file:

"hello

 

sdgHello

 

Bye"

 

Stuff to search for and remove: "sdg".

 

(Sorry for my primitive description!)  Could this be done?  Would str_replace() be the right thing to use?

 

Many thanks,

 

- Oliver

Link to comment
Share on other sites

A string replace would work however it will also replace part words i.e if I was to search for 'and' and replace with & then it will replace:

band => b&

sand => s&

andy => &y

 

So you may be better off using a regex function such as preg_replace() http://uk.php.net/preg_replace

 

You will first need to read the contents of the file into a variable i.e

$contents = file_get_contents('/path/to/file.txt');

Perform your replacements and then write the new content back to the file.

Edit:

Thinking about it you are better off reading the file contents using fopen() and then writing back with fwrite() as you have a resource handle to the file. http://uk.php.net/fwrite

 

Link to comment
Share on other sites

$file=file_get_contents("./file.txt");

$new_file=str_replace("sdg","", $file);

file_put_contents("./file.txt",$new_file,LOCK_EX);

 

This will do what you want but you might want to consider using preg_replace as you can be more specific. Str_replace will if you used "is" replace not only ishello to hello but this to th.

 

What and why exactly are you doing this?

 

 

HTH

Teamatomic

Link to comment
Share on other sites

Ok, thanks.  Im trying to strip a line of code out of all of the files on my site, that is now surplus to requirements!  - A little house keeping...

 

preg_replace looks like the way to go.  Do I need to provide a regex statement for the pattern?  Just proving "sdf" throughs back an error:

 

preg_replace(): Delimiter must not be alphanumeric or backslash in /Users/odh/Sites/BDRN/fix.php

 

Thanks.

Link to comment
Share on other sites

You need a regex pattern i.e. To replace 'abc' with nothing then:

$subject = 'Hello abc';
$result = preg_replace('/\\babc\\b/', '', $subject);
print $result;

The \b are word boundaries so part words will not be replaced. If you are wanting to replace part words then you should use str_replace()

Link to comment
Share on other sites

I'm just gonna say you have to be very careful and know what is in your files and if what you search for is going to be part of another word that matters

ie. str_replace "re" with ""  will make <a href= broken. but if you want to \bre\b then rehello will not change.

 

If this is a production server on linux/unix and you have shell/ssh access then do a search on "perl one liners" and as you munge files also make a .bak of it.

 

 

HTH

Teamatomic

Link to comment
Share on other sites

Fantastic, getting there!  Thanks for your help so far - reading through regex tutorials now!

 

So my code reades:

$problem = "<?php /**/ stuff ?>";
$contents = file_get_contents('test.txt');
$result = preg_replace("/\\b".$problem."\\b/",'',$contents);
print_r($result);
echo "Done";

 

and works fine for anything normal in $problem.  However i get an: 

 

Warning: preg_replace(): Unknown modifier '*' in /Users/odh/Sites/BDRN/fix.php on line 10

 

I tried adding a backslash in before each of the * but it then complains about the \.  Why would that be?  Does it see it as part of the expression?

 

Thanks.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.