|
7 | 7 |
|
8 | 8 | namespace Safe;
|
9 | 9 |
|
| 10 | +use const PREG_NO_ERROR; |
10 | 11 | use Safe\Exceptions\ApcException;
|
11 | 12 | use Safe\Exceptions\ApcuException;
|
12 | 13 | use Safe\Exceptions\JsonException;
|
| 14 | +use Safe\Exceptions\PcreException; |
13 | 15 |
|
14 | 16 | /**
|
15 | 17 | * Wrapper for json_decode that throws when an error occurs.
|
@@ -73,3 +75,88 @@ function apcu_fetch($key)
|
73 | 75 | }
|
74 | 76 | return $result;
|
75 | 77 | }
|
| 78 | + |
| 79 | +/** |
| 80 | + * Searches subject for matches to |
| 81 | + * pattern and replaces them with |
| 82 | + * replacement. |
| 83 | + * |
| 84 | + * @param mixed $pattern The pattern to search for. It can be either a string or an array with |
| 85 | + * strings. |
| 86 | + * |
| 87 | + * Several PCRE modifiers |
| 88 | + * are also available. |
| 89 | + * @param mixed $replacement The string or an array with strings to replace. If this parameter is a |
| 90 | + * string and the pattern parameter is an array, |
| 91 | + * all patterns will be replaced by that string. If both |
| 92 | + * pattern and replacement |
| 93 | + * parameters are arrays, each pattern will be |
| 94 | + * replaced by the replacement counterpart. If |
| 95 | + * there are fewer elements in the replacement |
| 96 | + * array than in the pattern array, any extra |
| 97 | + * patterns will be replaced by an empty string. |
| 98 | + * |
| 99 | + * replacement may contain references of the form |
| 100 | + * \\n or |
| 101 | + * $n, with the latter form |
| 102 | + * being the preferred one. Every such reference will be replaced by the text |
| 103 | + * captured by the n'th parenthesized pattern. |
| 104 | + * n can be from 0 to 99, and |
| 105 | + * \\0 or $0 refers to the text matched |
| 106 | + * by the whole pattern. Opening parentheses are counted from left to right |
| 107 | + * (starting from 1) to obtain the number of the capturing subpattern. |
| 108 | + * To use backslash in replacement, it must be doubled |
| 109 | + * ("\\\\" PHP string). |
| 110 | + * |
| 111 | + * When working with a replacement pattern where a backreference is |
| 112 | + * immediately followed by another number (i.e.: placing a literal number |
| 113 | + * immediately after a matched pattern), you cannot use the familiar |
| 114 | + * \\1 notation for your backreference. |
| 115 | + * \\11, for example, would confuse |
| 116 | + * preg_replace since it does not know whether you |
| 117 | + * want the \\1 backreference followed by a literal |
| 118 | + * 1, or the \\11 backreference |
| 119 | + * followed by nothing. In this case the solution is to use |
| 120 | + * ${1}1. This creates an isolated |
| 121 | + * $1 backreference, leaving the 1 |
| 122 | + * as a literal. |
| 123 | + * |
| 124 | + * When using the deprecated e modifier, this function escapes |
| 125 | + * some characters (namely ', ", |
| 126 | + * \ and NULL) in the strings that replace the |
| 127 | + * backreferences. This is done to ensure that no syntax errors arise |
| 128 | + * from backreference usage with either single or double quotes (e.g. |
| 129 | + * 'strlen(\'$1\')+strlen("$2")'). Make sure you are |
| 130 | + * aware of PHP's string |
| 131 | + * syntax to know exactly how the interpreted string will look. |
| 132 | + * @param string|array $subject The string or an array with strings to search and replace. |
| 133 | + * |
| 134 | + * If subject is an array, then the search and |
| 135 | + * replace is performed on every entry of subject, |
| 136 | + * and the return value is an array as well. |
| 137 | + * @param int $limit The maximum possible replacements for each pattern in each |
| 138 | + * subject string. Defaults to |
| 139 | + * -1 (no limit). |
| 140 | + * @param int $count If specified, this variable will be filled with the number of |
| 141 | + * replacements done. |
| 142 | + * @return string|array|null preg_replace returns an array if the |
| 143 | + * subject parameter is an array, or a string |
| 144 | + * otherwise. |
| 145 | + * |
| 146 | + * If matches are found, the new subject will |
| 147 | + * be returned, otherwise subject will be |
| 148 | + * returned unchanged. |
| 149 | + * |
| 150 | + * Returns a file pointer resource on success, . |
| 151 | + * @throws PcreException |
| 152 | + * |
| 153 | + */ |
| 154 | +function preg_replace($pattern, $replacement, $subject, int $limit = -1, int &$count = null) |
| 155 | +{ |
| 156 | + error_clear_last(); |
| 157 | + $result = \preg_replace($pattern, $replacement, $subject, $limit, $count); |
| 158 | + if (preg_last_error() !== PREG_NO_ERROR) { |
| 159 | + throw PcreException::createFromPhpError(); |
| 160 | + } |
| 161 | + return $result; |
| 162 | +} |
0 commit comments