Strings.match().phpt 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. /**
  3. * Test: Nette\Utils\Strings::match()
  4. */
  5. declare(strict_types=1);
  6. use Nette\Utils\Strings;
  7. use Tester\Assert;
  8. require __DIR__ . '/../bootstrap.php';
  9. Assert::null(Strings::match('hello world!', '#([E-L])+#'));
  10. Assert::same(['hell', 'l'], Strings::match('hello world!', '#([e-l])+#'));
  11. Assert::same(['hell'], Strings::match('hello world!', '#[e-l]+#'));
  12. Assert::same([[' ', 12]], Strings::match('россия - враг', '#\s+#u', PREG_OFFSET_CAPTURE));
  13. Assert::same([[' ', 12]], Strings::match('россия - враг', '#\s+#u', captureOffset: true));
  14. Assert::same([[' ', 6]], Strings::match('россия - враг', '#\s+#u', captureOffset: true, utf8: true));
  15. Assert::same(['e', null], Strings::match('hello world!', '#e(x)*#', unmatchedAsNull: true));
  16. Assert::same(['ll'], Strings::match('hello world!', '#[e-l]+#', offset: 2));
  17. Assert::same(['l'], Strings::match('žluťoučký kůň', '#[e-l]+#u', offset: 2));
  18. Assert::same(['k'], Strings::match('žluťoučký kůň', '#[e-l]+#u', utf8: true, offset: 2));
  19. Assert::same(['žluťoučký'], Strings::match('žluťoučký kůň', '#\w+#', utf8: true)); // without modifier
  20. Assert::same([['k', 7]], Strings::match('žluťoučký kůň', '#[e-l]+#u', captureOffset: true, utf8: true, offset: 2));
  21. Assert::null(Strings::match('hello world!', '', offset: 50));
  22. Assert::null(Strings::match('', '', offset: 1));