the_oliver Posted February 22, 2010 Share Posted February 22, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/192958-search-and-delete-within-a-file/ Share on other sites More sharing options...
JonnoTheDev Posted February 22, 2010 Share Posted February 22, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/192958-search-and-delete-within-a-file/#findComment-1016187 Share on other sites More sharing options...
teamatomic Posted February 22, 2010 Share Posted February 22, 2010 $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 Quote Link to comment https://forums.phpfreaks.com/topic/192958-search-and-delete-within-a-file/#findComment-1016201 Share on other sites More sharing options...
the_oliver Posted February 22, 2010 Author Share Posted February 22, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/192958-search-and-delete-within-a-file/#findComment-1016206 Share on other sites More sharing options...
JonnoTheDev Posted February 22, 2010 Share Posted February 22, 2010 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() Quote Link to comment https://forums.phpfreaks.com/topic/192958-search-and-delete-within-a-file/#findComment-1016213 Share on other sites More sharing options...
teamatomic Posted February 22, 2010 Share Posted February 22, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/192958-search-and-delete-within-a-file/#findComment-1016231 Share on other sites More sharing options...
the_oliver Posted February 22, 2010 Author Share Posted February 22, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/192958-search-and-delete-within-a-file/#findComment-1016240 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.