| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_DEFENDER_VENDOR\Safe; |
| 4 |
|
| 5 |
use WP_DEFENDER_VENDOR\Safe\Exceptions\PcreException; |
| 6 |
|
| 7 |
/** |
| 8 |
* Searches subject for all matches to the regular |
| 9 |
* expression given in pattern and puts them in |
| 10 |
* matches in the order specified by |
| 11 |
* flags. |
| 12 |
* |
| 13 |
* After the first match is found, the subsequent searches are continued |
| 14 |
* on from end of the last match. |
| 15 |
* |
| 16 |
* @param string $pattern The pattern to search for, as a string. |
| 17 |
* @param string $subject The input string. |
| 18 |
* @param array|null $matches Array of all matches in multi-dimensional array ordered according to |
| 19 |
* flags. |
| 20 |
* @param int $flags Can be a combination of the following flags (note that it doesn't make |
| 21 |
* sense to use PREG_PATTERN_ORDER together with |
| 22 |
* PREG_SET_ORDER): |
| 23 |
* |
| 24 |
* |
| 25 |
* PREG_PATTERN_ORDER |
| 26 |
* |
| 27 |
* |
| 28 |
* Orders results so that $matches[0] is an array of full |
| 29 |
* pattern matches, $matches[1] is an array of strings matched by |
| 30 |
* the first parenthesized subpattern, and so on. |
| 31 |
* |
| 32 |
* |
| 33 |
* |
| 34 |
* |
| 35 |
* |
| 36 |
* ]]> |
| 37 |
* |
| 38 |
* The above example will output: |
| 39 |
* |
| 40 |
* example: , this is a test |
| 41 |
* example: , this is a test |
| 42 |
* ]]> |
| 43 |
* |
| 44 |
* |
| 45 |
* So, $out[0] contains array of strings that matched full pattern, |
| 46 |
* and $out[1] contains array of strings enclosed by tags. |
| 47 |
* |
| 48 |
* |
| 49 |
* |
| 50 |
* |
| 51 |
* If the pattern contains named subpatterns, $matches |
| 52 |
* additionally contains entries for keys with the subpattern name. |
| 53 |
* |
| 54 |
* |
| 55 |
* If the pattern contains duplicate named subpatterns, only the rightmost |
| 56 |
* subpattern is stored in $matches[NAME]. |
| 57 |
* |
| 58 |
* |
| 59 |
* |
| 60 |
* ]]> |
| 61 |
* |
| 62 |
* The above example will output: |
| 63 |
* |
| 64 |
* |
| 65 |
* [1] => bar |
| 66 |
* ) |
| 67 |
* ]]> |
| 68 |
* |
| 69 |
* |
| 70 |
* |
| 71 |
* |
| 72 |
* |
| 73 |
* |
| 74 |
* PREG_SET_ORDER |
| 75 |
* |
| 76 |
* |
| 77 |
* Orders results so that $matches[0] is an array of first set |
| 78 |
* of matches, $matches[1] is an array of second set of matches, |
| 79 |
* and so on. |
| 80 |
* |
| 81 |
* |
| 82 |
* |
| 83 |
* ]]> |
| 84 |
* |
| 85 |
* The above example will output: |
| 86 |
* |
| 87 |
* example: , example: |
| 88 |
* this is a test, this is a test |
| 89 |
* ]]> |
| 90 |
* |
| 91 |
* |
| 92 |
* |
| 93 |
* |
| 94 |
* |
| 95 |
* |
| 96 |
* PREG_OFFSET_CAPTURE |
| 97 |
* |
| 98 |
* |
| 99 |
* If this flag is passed, for every occurring match the appendant string |
| 100 |
* offset (in bytes) will also be returned. Note that this changes the value of |
| 101 |
* matches into an array of arrays where every element is an |
| 102 |
* array consisting of the matched string at offset 0 |
| 103 |
* and its string offset into subject at offset |
| 104 |
* 1. |
| 105 |
* |
| 106 |
* |
| 107 |
* |
| 108 |
* ]]> |
| 109 |
* |
| 110 |
* The above example will output: |
| 111 |
* |
| 112 |
* Array |
| 113 |
* ( |
| 114 |
* [0] => Array |
| 115 |
* ( |
| 116 |
* [0] => foobarbaz |
| 117 |
* [1] => 0 |
| 118 |
* ) |
| 119 |
* |
| 120 |
* ) |
| 121 |
* |
| 122 |
* [1] => Array |
| 123 |
* ( |
| 124 |
* [0] => Array |
| 125 |
* ( |
| 126 |
* [0] => foo |
| 127 |
* [1] => 0 |
| 128 |
* ) |
| 129 |
* |
| 130 |
* ) |
| 131 |
* |
| 132 |
* [2] => Array |
| 133 |
* ( |
| 134 |
* [0] => Array |
| 135 |
* ( |
| 136 |
* [0] => bar |
| 137 |
* [1] => 3 |
| 138 |
* ) |
| 139 |
* |
| 140 |
* ) |
| 141 |
* |
| 142 |
* [3] => Array |
| 143 |
* ( |
| 144 |
* [0] => Array |
| 145 |
* ( |
| 146 |
* [0] => baz |
| 147 |
* [1] => 6 |
| 148 |
* ) |
| 149 |
* |
| 150 |
* ) |
| 151 |
* |
| 152 |
* ) |
| 153 |
* ]]> |
| 154 |
* |
| 155 |
* |
| 156 |
* |
| 157 |
* |
| 158 |
* |
| 159 |
* |
| 160 |
* PREG_UNMATCHED_AS_NULL |
| 161 |
* |
| 162 |
* |
| 163 |
* If this flag is passed, unmatched subpatterns are reported as NULL; |
| 164 |
* otherwise they are reported as an empty string. |
| 165 |
* |
| 166 |
* |
| 167 |
* |
| 168 |
* |
| 169 |
* |
| 170 |
* Orders results so that $matches[0] is an array of full |
| 171 |
* pattern matches, $matches[1] is an array of strings matched by |
| 172 |
* the first parenthesized subpattern, and so on. |
| 173 |
* |
| 174 |
* |
| 175 |
* |
| 176 |
* |
| 177 |
* ]]> |
| 178 |
* |
| 179 |
* The above example will output: |
| 180 |
* |
| 181 |
* example: , this is a test |
| 182 |
* example: , this is a test |
| 183 |
* ]]> |
| 184 |
* |
| 185 |
* |
| 186 |
* So, $out[0] contains array of strings that matched full pattern, |
| 187 |
* and $out[1] contains array of strings enclosed by tags. |
| 188 |
* |
| 189 |
* |
| 190 |
* |
| 191 |
* The above example will output: |
| 192 |
* |
| 193 |
* So, $out[0] contains array of strings that matched full pattern, |
| 194 |
* and $out[1] contains array of strings enclosed by tags. |
| 195 |
* |
| 196 |
* If the pattern contains named subpatterns, $matches |
| 197 |
* additionally contains entries for keys with the subpattern name. |
| 198 |
* |
| 199 |
* If the pattern contains duplicate named subpatterns, only the rightmost |
| 200 |
* subpattern is stored in $matches[NAME]. |
| 201 |
* |
| 202 |
* |
| 203 |
* |
| 204 |
* ]]> |
| 205 |
* |
| 206 |
* The above example will output: |
| 207 |
* |
| 208 |
* |
| 209 |
* [1] => bar |
| 210 |
* ) |
| 211 |
* ]]> |
| 212 |
* |
| 213 |
* |
| 214 |
* |
| 215 |
* The above example will output: |
| 216 |
* |
| 217 |
* Orders results so that $matches[0] is an array of first set |
| 218 |
* of matches, $matches[1] is an array of second set of matches, |
| 219 |
* and so on. |
| 220 |
* |
| 221 |
* |
| 222 |
* |
| 223 |
* ]]> |
| 224 |
* |
| 225 |
* The above example will output: |
| 226 |
* |
| 227 |
* example: , example: |
| 228 |
* this is a test, this is a test |
| 229 |
* ]]> |
| 230 |
* |
| 231 |
* |
| 232 |
* |
| 233 |
* The above example will output: |
| 234 |
* |
| 235 |
* If this flag is passed, for every occurring match the appendant string |
| 236 |
* offset (in bytes) will also be returned. Note that this changes the value of |
| 237 |
* matches into an array of arrays where every element is an |
| 238 |
* array consisting of the matched string at offset 0 |
| 239 |
* and its string offset into subject at offset |
| 240 |
* 1. |
| 241 |
* |
| 242 |
* |
| 243 |
* |
| 244 |
* ]]> |
| 245 |
* |
| 246 |
* The above example will output: |
| 247 |
* |
| 248 |
* Array |
| 249 |
* ( |
| 250 |
* [0] => Array |
| 251 |
* ( |
| 252 |
* [0] => foobarbaz |
| 253 |
* [1] => 0 |
| 254 |
* ) |
| 255 |
* |
| 256 |
* ) |
| 257 |
* |
| 258 |
* [1] => Array |
| 259 |
* ( |
| 260 |
* [0] => Array |
| 261 |
* ( |
| 262 |
* [0] => foo |
| 263 |
* [1] => 0 |
| 264 |
* ) |
| 265 |
* |
| 266 |
* ) |
| 267 |
* |
| 268 |
* [2] => Array |
| 269 |
* ( |
| 270 |
* [0] => Array |
| 271 |
* ( |
| 272 |
* [0] => bar |
| 273 |
* [1] => 3 |
| 274 |
* ) |
| 275 |
* |
| 276 |
* ) |
| 277 |
* |
| 278 |
* [3] => Array |
| 279 |
* ( |
| 280 |
* [0] => Array |
| 281 |
* ( |
| 282 |
* [0] => baz |
| 283 |
* [1] => 6 |
| 284 |
* ) |
| 285 |
* |
| 286 |
* ) |
| 287 |
* |
| 288 |
* ) |
| 289 |
* ]]> |
| 290 |
* |
| 291 |
* |
| 292 |
* |
| 293 |
* The above example will output: |
| 294 |
* |
| 295 |
* If this flag is passed, unmatched subpatterns are reported as NULL; |
| 296 |
* otherwise they are reported as an empty string. |
| 297 |
* |
| 298 |
* If no order flag is given, PREG_PATTERN_ORDER is |
| 299 |
* assumed. |
| 300 |
* @param int $offset Orders results so that $matches[0] is an array of full |
| 301 |
* pattern matches, $matches[1] is an array of strings matched by |
| 302 |
* the first parenthesized subpattern, and so on. |
| 303 |
* |
| 304 |
* |
| 305 |
* |
| 306 |
* |
| 307 |
* ]]> |
| 308 |
* |
| 309 |
* The above example will output: |
| 310 |
* |
| 311 |
* example: , this is a test |
| 312 |
* example: , this is a test |
| 313 |
* ]]> |
| 314 |
* |
| 315 |
* |
| 316 |
* So, $out[0] contains array of strings that matched full pattern, |
| 317 |
* and $out[1] contains array of strings enclosed by tags. |
| 318 |
* |
| 319 |
* |
| 320 |
* |
| 321 |
* The above example will output: |
| 322 |
* |
| 323 |
* So, $out[0] contains array of strings that matched full pattern, |
| 324 |
* and $out[1] contains array of strings enclosed by tags. |
| 325 |
* |
| 326 |
* If the pattern contains named subpatterns, $matches |
| 327 |
* additionally contains entries for keys with the subpattern name. |
| 328 |
* |
| 329 |
* If the pattern contains duplicate named subpatterns, only the rightmost |
| 330 |
* subpattern is stored in $matches[NAME]. |
| 331 |
* |
| 332 |
* |
| 333 |
* |
| 334 |
* ]]> |
| 335 |
* |
| 336 |
* The above example will output: |
| 337 |
* |
| 338 |
* |
| 339 |
* [1] => bar |
| 340 |
* ) |
| 341 |
* ]]> |
| 342 |
* |
| 343 |
* |
| 344 |
* |
| 345 |
* The above example will output: |
| 346 |
* |
| 347 |
* @return int Returns the number of full pattern matches (which might be zero). |
| 348 |
* @throws PcreException |
| 349 |
* |
| 350 |
*/ |
| 351 |
function preg_match_all(string $pattern, string $subject, ?array &$matches = null, int $flags = PREG_PATTERN_ORDER, int $offset = 0): int |
| 352 |
{ |
| 353 |
error_clear_last(); |
| 354 |
$result = \preg_match_all($pattern, $subject, $matches, $flags, $offset); |
| 355 |
if ($result === false) { |
| 356 |
throw PcreException::createFromPhpError(); |
| 357 |
} |
| 358 |
return $result; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
/** |
| 363 |
* Searches subject for a match to the regular |
| 364 |
* expression given in pattern. |
| 365 |
* |
| 366 |
* @param string $pattern The pattern to search for, as a string. |
| 367 |
* @param string $subject The input string. |
| 368 |
* @param array|null $matches If matches is provided, then it is filled with |
| 369 |
* the results of search. $matches[0] will contain the |
| 370 |
* text that matched the full pattern, $matches[1] |
| 371 |
* will have the text that matched the first captured parenthesized |
| 372 |
* subpattern, and so on. |
| 373 |
* @param int $flags flags can be a combination of the following flags: |
| 374 |
* |
| 375 |
* |
| 376 |
* PREG_OFFSET_CAPTURE |
| 377 |
* |
| 378 |
* |
| 379 |
* If this flag is passed, for every occurring match the appendant string |
| 380 |
* offset (in bytes) will also be returned. Note that this changes the value of |
| 381 |
* matches into an array where every element is an |
| 382 |
* array consisting of the matched string at offset 0 |
| 383 |
* and its string offset into subject at offset |
| 384 |
* 1. |
| 385 |
* |
| 386 |
* |
| 387 |
* |
| 388 |
* ]]> |
| 389 |
* |
| 390 |
* The above example will output: |
| 391 |
* |
| 392 |
* Array |
| 393 |
* ( |
| 394 |
* [0] => foobarbaz |
| 395 |
* [1] => 0 |
| 396 |
* ) |
| 397 |
* |
| 398 |
* [1] => Array |
| 399 |
* ( |
| 400 |
* [0] => foo |
| 401 |
* [1] => 0 |
| 402 |
* ) |
| 403 |
* |
| 404 |
* [2] => Array |
| 405 |
* ( |
| 406 |
* [0] => bar |
| 407 |
* [1] => 3 |
| 408 |
* ) |
| 409 |
* |
| 410 |
* [3] => Array |
| 411 |
* ( |
| 412 |
* [0] => baz |
| 413 |
* [1] => 6 |
| 414 |
* ) |
| 415 |
* |
| 416 |
* ) |
| 417 |
* ]]> |
| 418 |
* |
| 419 |
* |
| 420 |
* |
| 421 |
* |
| 422 |
* |
| 423 |
* |
| 424 |
* PREG_UNMATCHED_AS_NULL |
| 425 |
* |
| 426 |
* |
| 427 |
* If this flag is passed, unmatched subpatterns are reported as NULL; |
| 428 |
* otherwise they are reported as an empty string. |
| 429 |
* |
| 430 |
* |
| 431 |
* |
| 432 |
* ]]> |
| 433 |
* |
| 434 |
* The above example will output: |
| 435 |
* |
| 436 |
* |
| 437 |
* string(2) "ac" |
| 438 |
* [1]=> |
| 439 |
* string(1) "a" |
| 440 |
* [2]=> |
| 441 |
* string(0) "" |
| 442 |
* [3]=> |
| 443 |
* string(1) "c" |
| 444 |
* } |
| 445 |
* array(4) { |
| 446 |
* [0]=> |
| 447 |
* string(2) "ac" |
| 448 |
* [1]=> |
| 449 |
* string(1) "a" |
| 450 |
* [2]=> |
| 451 |
* NULL |
| 452 |
* [3]=> |
| 453 |
* string(1) "c" |
| 454 |
* } |
| 455 |
* ]]> |
| 456 |
* |
| 457 |
* |
| 458 |
* |
| 459 |
* |
| 460 |
* |
| 461 |
* |
| 462 |
* |
| 463 |
* If this flag is passed, for every occurring match the appendant string |
| 464 |
* offset (in bytes) will also be returned. Note that this changes the value of |
| 465 |
* matches into an array where every element is an |
| 466 |
* array consisting of the matched string at offset 0 |
| 467 |
* and its string offset into subject at offset |
| 468 |
* 1. |
| 469 |
* |
| 470 |
* |
| 471 |
* |
| 472 |
* ]]> |
| 473 |
* |
| 474 |
* The above example will output: |
| 475 |
* |
| 476 |
* Array |
| 477 |
* ( |
| 478 |
* [0] => foobarbaz |
| 479 |
* [1] => 0 |
| 480 |
* ) |
| 481 |
* |
| 482 |
* [1] => Array |
| 483 |
* ( |
| 484 |
* [0] => foo |
| 485 |
* [1] => 0 |
| 486 |
* ) |
| 487 |
* |
| 488 |
* [2] => Array |
| 489 |
* ( |
| 490 |
* [0] => bar |
| 491 |
* [1] => 3 |
| 492 |
* ) |
| 493 |
* |
| 494 |
* [3] => Array |
| 495 |
* ( |
| 496 |
* [0] => baz |
| 497 |
* [1] => 6 |
| 498 |
* ) |
| 499 |
* |
| 500 |
* ) |
| 501 |
* ]]> |
| 502 |
* |
| 503 |
* |
| 504 |
* |
| 505 |
* The above example will output: |
| 506 |
* |
| 507 |
* If this flag is passed, unmatched subpatterns are reported as NULL; |
| 508 |
* otherwise they are reported as an empty string. |
| 509 |
* |
| 510 |
* |
| 511 |
* |
| 512 |
* ]]> |
| 513 |
* |
| 514 |
* The above example will output: |
| 515 |
* |
| 516 |
* |
| 517 |
* string(2) "ac" |
| 518 |
* [1]=> |
| 519 |
* string(1) "a" |
| 520 |
* [2]=> |
| 521 |
* string(0) "" |
| 522 |
* [3]=> |
| 523 |
* string(1) "c" |
| 524 |
* } |
| 525 |
* array(4) { |
| 526 |
* [0]=> |
| 527 |
* string(2) "ac" |
| 528 |
* [1]=> |
| 529 |
* string(1) "a" |
| 530 |
* [2]=> |
| 531 |
* NULL |
| 532 |
* [3]=> |
| 533 |
* string(1) "c" |
| 534 |
* } |
| 535 |
* ]]> |
| 536 |
* |
| 537 |
* |
| 538 |
* |
| 539 |
* The above example will output: |
| 540 |
* @param int $offset If this flag is passed, for every occurring match the appendant string |
| 541 |
* offset (in bytes) will also be returned. Note that this changes the value of |
| 542 |
* matches into an array where every element is an |
| 543 |
* array consisting of the matched string at offset 0 |
| 544 |
* and its string offset into subject at offset |
| 545 |
* 1. |
| 546 |
* |
| 547 |
* |
| 548 |
* |
| 549 |
* ]]> |
| 550 |
* |
| 551 |
* The above example will output: |
| 552 |
* |
| 553 |
* Array |
| 554 |
* ( |
| 555 |
* [0] => foobarbaz |
| 556 |
* [1] => 0 |
| 557 |
* ) |
| 558 |
* |
| 559 |
* [1] => Array |
| 560 |
* ( |
| 561 |
* [0] => foo |
| 562 |
* [1] => 0 |
| 563 |
* ) |
| 564 |
* |
| 565 |
* [2] => Array |
| 566 |
* ( |
| 567 |
* [0] => bar |
| 568 |
* [1] => 3 |
| 569 |
* ) |
| 570 |
* |
| 571 |
* [3] => Array |
| 572 |
* ( |
| 573 |
* [0] => baz |
| 574 |
* [1] => 6 |
| 575 |
* ) |
| 576 |
* |
| 577 |
* ) |
| 578 |
* ]]> |
| 579 |
* |
| 580 |
* |
| 581 |
* |
| 582 |
* The above example will output: |
| 583 |
* |
| 584 |
* @return int preg_match returns 1 if the pattern |
| 585 |
* matches given subject, 0 if it does not. |
| 586 |
* @throws PcreException |
| 587 |
* |
| 588 |
*/ |
| 589 |
function preg_match(string $pattern, string $subject, ?array &$matches = null, int $flags = 0, int $offset = 0): int |
| 590 |
{ |
| 591 |
error_clear_last(); |
| 592 |
$result = \preg_match($pattern, $subject, $matches, $flags, $offset); |
| 593 |
if ($result === false) { |
| 594 |
throw PcreException::createFromPhpError(); |
| 595 |
} |
| 596 |
return $result; |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
/** |
| 601 |
* Split the given string by a regular expression. |
| 602 |
* |
| 603 |
* @param string $pattern The pattern to search for, as a string. |
| 604 |
* @param string $subject The input string. |
| 605 |
* @param int|null $limit If specified, then only substrings up to limit |
| 606 |
* are returned with the rest of the string being placed in the last |
| 607 |
* substring. A limit of -1 or 0 means "no limit". |
| 608 |
* @param int $flags flags can be any combination of the following |
| 609 |
* flags (combined with the | bitwise operator): |
| 610 |
* |
| 611 |
* |
| 612 |
* PREG_SPLIT_NO_EMPTY |
| 613 |
* |
| 614 |
* |
| 615 |
* If this flag is set, only non-empty pieces will be returned by |
| 616 |
* preg_split. |
| 617 |
* |
| 618 |
* |
| 619 |
* |
| 620 |
* |
| 621 |
* PREG_SPLIT_DELIM_CAPTURE |
| 622 |
* |
| 623 |
* |
| 624 |
* If this flag is set, parenthesized expression in the delimiter pattern |
| 625 |
* will be captured and returned as well. |
| 626 |
* |
| 627 |
* |
| 628 |
* |
| 629 |
* |
| 630 |
* PREG_SPLIT_OFFSET_CAPTURE |
| 631 |
* |
| 632 |
* |
| 633 |
* If this flag is set, for every occurring match the appendant string |
| 634 |
* offset will also be returned. Note that this changes the return |
| 635 |
* value in an array where every element is an array consisting of the |
| 636 |
* matched string at offset 0 and its string offset |
| 637 |
* into subject at offset 1. |
| 638 |
* |
| 639 |
* |
| 640 |
* |
| 641 |
* |
| 642 |
* |
| 643 |
* If this flag is set, for every occurring match the appendant string |
| 644 |
* offset will also be returned. Note that this changes the return |
| 645 |
* value in an array where every element is an array consisting of the |
| 646 |
* matched string at offset 0 and its string offset |
| 647 |
* into subject at offset 1. |
| 648 |
* @return array Returns an array containing substrings of subject |
| 649 |
* split along boundaries matched by pattern. |
| 650 |
* @throws PcreException |
| 651 |
* |
| 652 |
*/ |
| 653 |
function preg_split(string $pattern, string $subject, ?int $limit = -1, int $flags = 0): array |
| 654 |
{ |
| 655 |
error_clear_last(); |
| 656 |
$result = \preg_split($pattern, $subject, $limit, $flags); |
| 657 |
if ($result === false) { |
| 658 |
throw PcreException::createFromPhpError(); |
| 659 |
} |
| 660 |
return $result; |
| 661 |
} |
| 662 |
|