File: preg_replace_callback_basic.phpt

package info (click to toggle)
php7.0 7.0.33-0%2Bdeb9u8
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 184,180 kB
  • sloc: ansic: 809,195; php: 22,035; sh: 11,873; cpp: 8,064; xml: 6,228; javascript: 2,546; yacc: 1,869; exp: 1,514; makefile: 1,231; pascal: 1,200; awk: 501; perl: 315; sql: 22
file content (33 lines) | stat: -rw-r--r-- 1,496 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
--TEST--
Test preg_replace_callback() function : basic functionality 
--FILE--
<?php
/*
* proto string preg_replace(mixed regex, mixed replace, mixed subject [, int limit [, count]])
* Function is implemented in ext/pcre/php_pcre.c
*/
/*
* Basic test for preg_replace_callback
*/
$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.
function integer_word($matches) {
    global $replacement;
    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
    
}
$subject1 = 'there are 7 words in this sentence.';
$new_subject1 = preg_replace_callback('/\d/', "integer_word", $subject1);
print "$new_subject1 \n";
$subject2 = '1 2 3 4 is now written in words';
$new_subject2 = preg_replace_callback('/\d/', "integer_word", $subject2, 3); //limits to three replacements
print "$new_subject2 \n";
$subject3 = 'there are no numbers in this string';
$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
print "$new_subject3 \n";
print $count;
?>
--EXPECTF--
there are seven words in this sentence. 
one two three 4 is now written in words 
there are no numbers in this string 
0