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

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -