change a word in a line of text file with php -
i need change word after maildir, in exemple null of line : (the word null can word.)
[3]=> string(42) "spam * ^subject: \[spam\].* maildir/.null/"
i use code :
if($filecontent = file_get_contents($filename)){ $repertoire = "spam"; $tab = explode("#", trim($filecontent)); $tab[3] = preg_replace("#maildir/*#", 'maildir/.'.$repertoire.'/', $tab[3]); var_dump($tab);
but have
string(48) "spam * ^subject: \[spam\].* maildir/.null/.spam/"
how can change in maildir/.spam because .null in excess.
string(48) "spam * ^subject: \[spam\].* maildir/.spam/"
you can this:
preg_replace('~maildir/\k\.null/(?=\.spam)~', '', $string);
or word:
preg_replace('~maildir/\k[\w.-]++/(?=\.spam)~', '', $string);
i have tested with:
$string = <<<lod spam * ^subject: \[spam\].* maildir/.null/.spam/ lod; echo preg_replace('~maildir/\k[\w.-]++/(?=\.spam)~', '', $string).'<br/>'; echo preg_replace('~maildir/\k\.null/(?=\.spam)~', '', $string).'<br/>';
Comments
Post a Comment