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.
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)
|
Mik Mon Sep 5, 2005 9:50 am
Just what i wanted :)..
Thank's alot! |
|
testoffensive Fri Jan 18, 2008 1:23 am
testoffensiveonly
classified and lashiti |
|
testoffensive2 Fri Jan 18, 2008 1:25 am
I am sorry, but I want to test.
ass shit alafucki |
|
testoffensive3 Fri Jan 18, 2008 1:25 am
I am sorry again, but I am testing (you can delete these leter. thank you very much)
fuck test |
|
testoffensive7 Thu Feb 21, 2008 7:51 am
testing swedish offensive words.
korv mamma cykel choklad boll ...... snopp penis kuk |
|
napster Wed Jun 4, 2008 9:59 pm
this is good, but I need to edit from a file and save it again
|
|
Joshua Cheong Marketing Mon Apr 5, 2010 9: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 1:48 pm
Da pizda tvoje mame smrdljive, konjino jedna glupava, nemas poima!!!!!!!!! SAMO PRLJATE GOOGLE PIZDA VAM MATERINA!!!!!!!
|
|
boxy Sat Dec 4, 2010 9: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 9: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 9: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. |
©2004 - 2012 scriptplayground | Privacy Policy | Legal
Validate Site: XHTML CSS | Designed by: Matthew Keefe of mkeefeDESIGN