language: PHP (php 5.4.4)
date: 574 days 23 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
 
$map = array(   'foo' => 'FOO',
                'over' => 'OVER');
 
// get the keys.
$keys = array_keys($map);
 
// get the values.
$values = array_values($map);
 
// surround each key in word boundary and regex delimiter
// also escape any regex metachar in the key
foreach($keys as &$key) {
        $key = '/\b'.preg_quote($key).'\b/';
}
 
// input string.    
$str = 'Hi foo over the foobar in stackoverflow';
 
var_dump($str);
 
// do the replacement using preg_replace                
$str = preg_replace($keys,$values,$str);
 
var_dump($str);