$f = file('file.txt'); foreach($f as $row) { echo str_replace('[KEYWORD]', str_replace(' ','-',trim(strip_tags($row))), $row); }