-
Notifications
You must be signed in to change notification settings - Fork 0
/
smart_replace.php
32 lines (30 loc) · 1.21 KB
/
smart_replace.php
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
27
28
29
30
31
32
<?php
/*
* Smart Replace.
* --------------
* String replace PHP function with the option to exclude possibilities.
* $replace array can be as follows: [$searchfor => $replacewith]
*
* @param array $replace
* @param string $string
* @param array $exclude
*
* @return string
*/
function smart_replace($replace = [], $string = "", $exclude = []) {
$forward = $backward = [];
if (is_array($exclude) && count($exclude) > 0 && !empty($exclude)) {
foreach ($exclude as $excl) {
$exclString = str_replace(["<", ">"], "", $excl);
$exclEncryptedString = "[[" . md5($exclString) . "]]";
$forward = array_merge([$exclString => $exclEncryptedString], $forward);
$backward = array_merge([$exclEncryptedString => $exclString], $backward);
$string = str_replace(array_keys($forward), array_values($forward), $string);
}
}
$replacedString = str_replace(array_keys($replace), array_values($replace), $string);
if (is_array($exclude) && count($exclude) > 0 && !empty($exclude)) {
$replacedString = str_replace(array_keys($backward), array_values($backward), $replacedString);
}
return $replacedString;
}