|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) 2013-2017 |
|
4
|
|
|
* |
|
5
|
|
|
* @category Library |
|
6
|
|
|
* @package Dwoo\Plugins\Functions |
|
7
|
|
|
* @author Jordi Boggiano <[email protected]> |
|
8
|
|
|
* @author David Sanchez <[email protected]> |
|
9
|
|
|
* @copyright 2008-2013 Jordi Boggiano |
|
10
|
|
|
* @copyright 2013-2017 David Sanchez |
|
11
|
|
|
* @license http://dwoo.org/LICENSE Modified BSD License |
|
12
|
|
|
* @version 1.3.2 |
|
13
|
|
|
* @date 2017-01-06 |
|
14
|
|
|
* @link http://dwoo.org/ |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace Dwoo\Plugins\Functions; |
|
18
|
|
|
|
|
19
|
|
|
use Dwoo\Plugin; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Replaces the search string by the replace string using regular expressions |
|
23
|
|
|
* <pre> |
|
24
|
|
|
* * value : the string to search into |
|
25
|
|
|
* * search : the string to search for, must be a complete regular expression including delimiters |
|
26
|
|
|
* * replace : the string to use as a replacement, must be a complete regular expression including delimiters |
|
27
|
|
|
* </pre> |
|
28
|
|
|
* This software is provided 'as-is', without any express or implied warranty. |
|
29
|
|
|
* In no event will the authors be held liable for any damages arising from the use of this software. |
|
30
|
|
|
*/ |
|
31
|
|
|
class PluginRegexReplace extends Plugin |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @param string $value |
|
35
|
|
|
* @param string $search |
|
36
|
|
|
* @param string $replace |
|
37
|
|
|
* |
|
38
|
|
|
* @return mixed |
|
39
|
|
|
*/ |
|
40
|
|
|
public function process($value, $search, $replace) |
|
41
|
|
|
{ |
|
42
|
|
|
$search = (array)$search; |
|
43
|
|
|
$cnt = count($search); |
|
44
|
|
|
|
|
45
|
|
|
for ($i = 0; $i < $cnt; ++ $i) { |
|
46
|
|
|
// Credits for this to Monte Ohrt who made smarty's regex_replace modifier |
|
47
|
|
|
if (($pos = strpos($search[$i], "\0")) !== false) { |
|
48
|
|
|
$search[$i] = substr($search[$i], 0, $pos); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if (preg_match('#[a-z\s]+$#is', $search[$i], $m) && (strpos($m[0], 'e') !== false)) { |
|
52
|
|
|
$search[$i] = substr($search[$i], 0, - strlen($m[0])) . str_replace(array( |
|
53
|
|
|
'e', |
|
54
|
|
|
' ' |
|
55
|
|
|
), |
|
56
|
|
|
'', |
|
57
|
|
|
$m[0]); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return preg_replace($search, $replace, $value); |
|
62
|
|
|
} |
|
63
|
|
|
} |