If you don't want to be something very advanced, this is a simple solution:
Source of testfile.txt
abc def ghi
abc defghi
abc def ghi
abc dfghi
abc defdef ghi
PHP Code
<?php
// Get a file into an array.
$lines = file('testfile.txt');
// Loop through our array, show content and replace whatever is needed.
foreach ($lines as $line) {
$SearchAndReplace=str_replace('def', 'def 123', $line);
echo $SearchAndReplace.'<br />';
}
?>
Output is:
abc def 123 ghi
abc def 123ghi
abc def 123 ghi
abc dfghi
abc def 123def 123 ghi
You may wanna check the PHP documentation about file function