Annotation of embedaddon/php/ext/pcre/tests/preg_replace_callback_basic.phpt, revision 1.1.1.1
1.1 misho 1: --TEST--
2: Test preg_replace_callback() function : basic functionality
3: --FILE--
4: <?php
5: /*
6: * proto string preg_replace(mixed regex, mixed replace, mixed subject [, int limit [, count]])
7: * Function is implemented in ext/pcre/php_pcre.c
8: */
9: /*
10: * Basic test for preg_replace_callback
11: */
12: $replacement = array('zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'); //array will have the default key values (0-9) and each value is the corresponding key written in words.
13: function integer_word($matches) {
14: global $replacement;
15: return $replacement[$matches[0]]; //all examples will be looking for an integer value, if one is found it will be stored in $matches[0] which corresponds to a key in the $replacements array
16:
17: }
18: $subject1 = 'there are 7 words in this sentence.';
19: $new_subject1 = preg_replace_callback('/\d/', "integer_word", $subject1);
20: print "$new_subject1 \n";
21: $subject2 = '1 2 3 4 is now written in words';
22: $new_subject2 = preg_replace_callback('/\d/', "integer_word", $subject2, 3); //limits to three replacements
23: print "$new_subject2 \n";
24: $subject3 = 'there are no numbers in this string';
25: $new_subject3 = preg_replace_callback('/\d/', "integer_word", $subject3, 5, $count); //limites to five replacements and counts the number of replacements made ands stores in $count variable
26: print "$new_subject3 \n";
27: print $count;
28: ?>
29: --EXPECTF--
30: there are seven words in this sentence.
31: one two three 4 is now written in words
32: there are no numbers in this string
33: 0
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>