RegexTest.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2019/5/16
  6. * Time: 下午6:22
  7. */
  8. class RegexTest extends TestInit
  9. {
  10. public function testMatch()
  11. {
  12. $msg = 'http://sss.d/ssd/';
  13. $parse = parse_url($msg);
  14. var_dump(preg_match('/^sss/', $parse['host']));
  15. var_dump(parse_url($msg));
  16. }
  17. public function testReplace()
  18. {
  19. $msg = 'aaabbcccaadd';
  20. $match = ['aa','cc'];
  21. $replace = ['jj','hh'];
  22. var_dump($this->replaceOneByOne($msg, $match, $replace));
  23. }
  24. public static function replaceOneByOne($string, $fromList, $toList)
  25. {
  26. $offset = 0;
  27. foreach ($fromList as $index => $item) {
  28. $position = strpos($string, $item, $offset);
  29. if ($position !== false) {
  30. $left = substr($string, 0, $position);
  31. $right = substr($string, $position + strlen($item));
  32. $middle = $toList[$index];
  33. $string = $left . $middle . $right;
  34. $offset = strlen($left . $middle);
  35. }
  36. }
  37. return $string;
  38. }
  39. }