php - Replace everything from beginning of line to equal sign -
for content format:
key=value
like:
listen=i listening.
i need replacing using regex. want regular expression replace before = $key (making have beginning of line key 'en' wont replace key "token".
here's i'm using, doesn't seem work:
$content = preg_replace('~^'.$key.'\s?=[^\n$]+~iu',$newkey,$content);
$content = "foo=one\n" . "bar=two\n" . "baz=three\n"; $keys = array( 'foo' => 'newfoo', 'bar' => 'newbar', 'baz' => 'newbaz', ); foreach ( $keys $oldkey => $newkey ) { $oldkey = preg_quote($oldkey, '#'); $content = preg_replace("#^{$oldkey}( ?=)#m", "{$newkey}\\1", $content); } echo $content;
output:
newfoo=one newbar=two newbaz=three
Comments
Post a Comment