-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathregex-iterator.php
29 lines (27 loc) · 975 Bytes
/
regex-iterator.php
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
<?php
/**
* This is a very basic example of using the regex iterator to perform a
* replacement. In this case, we are merely performing a swap of the
* pattern matches (test) and (0-9+).
*
* You can specify any of the following modes:
*
* RegexIterator::MATCH Only execute match (filter) for the current entry.
* RegexIterator::GET_MATCH Return the first match for the current entry.
* RegexIterator::ALL_MATCHES Return all matches for the current entry.
* RegexIterator::SPLIT Returns the split values for the current entry.
* RegexIterator::REPLACE Replace the current entry.
* RegexIterator::USE_KEY Special flag: Match the entry key instead of the entry value.
*/
$a = new ArrayIterator(array('test1', 'test2', 'test3'));
$i = new RegexIterator($a, '/^(test)(\d+)/', RegexIterator::REPLACE);
$i->replacement = '$2:$1';
print_r(iterator_to_array($i));
/*
Array
(
[0] => 1:test
[1] => 2:test
[2] => 3:test
)
*/