preg_replace_edit_basic.phpt 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. --TEST--
  2. Test preg_replace() function : basic
  3. --FILE--
  4. <?php
  5. /* Prototype : proto string preg_replace(mixed regex, mixed replace, mixed subject [, int limit [, count]])
  6. * Description: Perform Perl-style regular expression replacement.
  7. * Source code: ext/pcre/php_pcre.c
  8. * Alias to functions:
  9. */
  10. $string = '123456789 - Hello, world - This is a string.';
  11. var_dump($string);
  12. var_dump(preg_replace('<- This is a string$>',
  13. 'This shouldn\'t work', $string)); //tries to find '- This is a string' at the end of a string but can't so replaces nothing and prints the unchanged $string.
  14. var_dump(preg_replace('<[0-35-9]>',
  15. '4', $string, //finds any number that's not 4 and replaces it with a 4
  16. '5', $count)); //limits to 5 replacements returns 444444789
  17. var_dump($count); //counts the number of replacements made (5)
  18. var_dump(preg_replace('<\b[hH]\w{2,4}>',
  19. 'Bonjour', $string)); //finds h or H at the beginning of a word followed by 2-4 characters and replaces it with Bonjour (i.e. Hello -> Bonjour) (was finding the 'his' in This and replacing it)
  20. var_dump(preg_replace('<(\w)\s*-\s*(\w)>',
  21. '\\1. \\2', $string)); //finds dashes with an indefinite amount of whitespace around them and replaces them with a full stop precedeby no spaces and followed by one space
  22. var_dump(preg_replace('<(^[a-z]\w+)@(\w+)\.(\w+)\.([a-z]{2,}$)>',
  23. '\\1 at \\2 dot \\3 dot \\4', 'josmessa@uk.ibm.com')); //finds the e-mail address and replaces the @ and . with "at" and "dot" (uses backreferences) ('josmessa at uk dot ibm dot com')
  24. ?>
  25. --EXPECTF--
  26. string(54) "123456789 - Hello, world - This is a string."
  27. string(54) "123456789 - Hello, world - This is a string."
  28. string(54) "444444789 - Hello, world - This is a string."
  29. int(5)
  30. string(56) "123456789 - Bonjour, world - This is a string."
  31. string(42) "123456789. Hello, world. This is a string."
  32. string(30) "josmessa at uk dot ibm dot com"