Subscribe to Tutorial Feed

Flash and PHP Bible

The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.

The Flash and PHP Bible has a forum for quick support.

Scriptplayground » tutorials » php » Search and Replace a Loaded File

Search and Replace a Loaded File

A simple to use script for loading a file, search and replacing words, and displaying the file.

The path to the file to open.

// File to open
$file = '/path/to/file/example.txt';

Set up the arrays to hold the search and replace words. Seperate each word with a comma and place each one in ("")

// Search and Replace Arrays
$search = array ('beer','pizza','hamburger','ice','food');
$replace = array ('soda','calzone','hotdog','water','drink');

Set up a handler to open the file and open it.

// Read through the file
$lines = file($file);

Begin reading the file and start searching through it line for line.

foreach ($lines as $line_num => $line) 
	{

Replace the words using the search/replace arrays.

	$text = preg_replace($search, $replace, $line);

Print the results to the screen, you could also output these results to a new file. The choice is yours.

	print $text;
	}

Here is the code in full, enjoy.

<?

// File to open
$file = '/path/to/file/example.txt';

// Search and Replace Arrays
$search = array('beer','pizza','hamburger','ice','food');
$replace = array('soda','calzone','hotdog','water','drink');

// Open the file
$lines = file($file);

// Read through the file
foreach($lines as $line_num => $line) {
    $text = preg_replace($search, $replace, $line);
    print $text;
}

?>

That is the end of the article, until next time... happy scripting.

Follow Scriptplayground on Twitter (@scriptplay)

Comments: Search and Replace a Loaded File

 Mik  Mon Sep 5, 2005 5:50 am  
Just what i wanted :)..

Thank's alot!
 testoffensive  Thu Jan 17, 2008 8:23 pm  
testoffensiveonly

classified and lashiti
 testoffensive2  Thu Jan 17, 2008 8:25 pm  
I am sorry, but I want to test.

ass shit alafucki
 testoffensive3  Thu Jan 17, 2008 8:25 pm  
I am sorry again, but I am testing (you can delete these leter. thank you very much)

fuck test
 testoffensive7  Thu Feb 21, 2008 2:51 am  
testing swedish offensive words.

korv
mamma
cykel
choklad
boll
......
snopp
penis
kuk
 napster  Wed Jun 4, 2008 5:59 pm  
this is good, but I need to edit from a file and save it again
 Joshua Cheong Marketing  Mon Apr 5, 2010 5:13 am  
I got an error using the script code:

'Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in...'

Can anyone help me?
 jebac  Sun Apr 25, 2010 9:48 am  
Da pizda tvoje mame smrdljive, konjino jedna glupava, nemas poima!!!!!!!!! SAMO PRLJATE GOOGLE PIZDA VAM MATERINA!!!!!!!
 boxy  Sat Dec 4, 2010 4:05 am  
I got an error using the script code:

'Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in...'

Can anyone help me?


$search = array('beer','pizza','hamburger','ice','food');

replace with

$search = array('beer','pizza','hamburger','ice','food');


 boxy  Sat Dec 4, 2010 4:06 am  
I got an error using the script code:

'Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in...'

Can anyone help me?


$search = array('beer','pizza','hamburger','ice','food');

replace with

$search = array('beer','pizza','hamburger','ice','food');

 boxy  Sat Dec 4, 2010 4:07 am  
Here is the code in full, enjoy.


// File to open
$file = '/path/to/file/example.txt';

// Search and Replace Arrays
$search = array('/beer/','/pizza/','/hamburger/','/ice/','/food/');
$replace = array('soda','calzone','hotdog','water','drink');

// Open the file
$lines = file($file);

// Read through the file
foreach($lines as $line_num => $line) {
$text = preg_replace($search, $replace, $line);
print $text;
}

?>
That is the end of the article, until next time... happy scripting.

Add a comment
The ability to add new comments has been disabled in this archived version.

Loading
Main | Tutorials | Articles