Instance Code
$string = ' key An array group can contain both integer and string key names, 12345678 because PHP does not actually differentiate between indexed arrays and associative arrays. If no key name is specified for the given value, the current largest integer index value is taken, and the new key name is the value plus one. If the specified key name already has a value, the value is overwritten. '; $keyArray =array (' array ', ' Integer ', ' 2345 ', ' Key Name '); $replacement =array (' 0 ' =>array (' 0 ' + ') ' key ', ' 1 ' = ' = ' Www.baidu.com ", ' 2 ' = ' 2 '), ' 1 ' =>array (' 0 ' = ' = ' array can ', ' 1 ' = ' http://www.baidu.com ', ' 2 ' = ' 2 '), ' 2 ' = = Array (' 0 ' = ' + ' arrays ', ' 1 ' = ' = ' http://www.baidu.com ', ' 2 ' = ' 1 ')); can achieve the control of the number of replacements, not only to replace only once, for example, $limit is 2 when a word appears a lot of time to eat only replace 2 times,//-1 means to replace all. Both $search and $replace can be strings or arrays, but must correspond to function Str_replace_limit ($search, $replace, $content, $limit =-1) {if (Is_array ($ Search) {foreach ($search as $k = + $v) {$search [$k]= '. Preg_quote ($search [$k], '). ' `'; }}else{$search = "". Preg_quote ($search, "). ' `'; } $content =preg_replace ("/alt= ([^ >]+)/is", ", $content); Return Preg_replace ($search, $replace, $content, $limit); }function keylinks ($string, $replacement) {for ($i =0; $i
Desired features
The second time has replaced the array but replaced it with the third time.
Want to achieve is the second time to replace the time there is no way to skip the already replaced
Reply to discussion (solution)
foreach ($replacement as $v) { $p = '/([^>]) ('. Preg_quote ($v [0]). ') ([^<])/s '; $r = "$1$2$3"; $n = $v [2]; $string = Preg_replace ($p, $r, $string, $n);} Echo $string;
The key an array group can contain both integer and string key names, 12345678 because PHP does not actually differentiate between indexed arrays and associative arrays.
If no key name is specified for the given value, the current largest integer index value is taken, and the new key name is the value plus one. If the specified key name already has a value, the value is overwritten.
foreach ($replacement as $v) { $p = '/([^>]) ('. Preg_quote ($v [0]). ') ([^<])/s '; $r = "$1$2$3"; $n = $v [2]; $string = Preg_replace ($p, $r, $string, $n);} Echo $string;
The key an array group can contain both integer and string key names, 12345678 because PHP does not actually differentiate between indexed arrays and associative arrays.
If no key name is specified for the given value, the current largest integer index value is taken, and the new key name is the value plus one. If the specified key name already has a value, the value is overwritten.
Tried this method, but it didn't work. I don't know if there's any other way to do it.