1 | <?php |
---|
2 | function test_regexp($match) { |
---|
3 | $return = array_shift($match); |
---|
4 | #echo "\n"; |
---|
5 | #var_export($match); |
---|
6 | #echo "\n"; |
---|
7 | $begin = array_shift($match); |
---|
8 | $end = array_pop($match); |
---|
9 | #echo "\n"; |
---|
10 | #var_export($match); |
---|
11 | #echo "\n"; |
---|
12 | if ( $begin == '[' && $end == ']' ) { |
---|
13 | return substr($return, 1, strlen($return) - 2); |
---|
14 | } else { |
---|
15 | $content = array_pop($match); |
---|
16 | $tag = array_shift($match); |
---|
17 | $attr = array_shift($match); |
---|
18 | #var_dump($begin, $end); |
---|
19 | if ( $content ) { |
---|
20 | return $begin . '{' . $tag . $attr . '}' . $content . '{/' . $tag . '}' . $end; |
---|
21 | } else { |
---|
22 | return $begin . '{' . $tag . $attr . '/}' . $end; |
---|
23 | } |
---|
24 | } |
---|
25 | } |
---|
26 | |
---|
27 | $tests = array( |
---|
28 | '[foo]bar[/foo]' => '{foo}bar{/foo}', |
---|
29 | '[foo/]bar[/foo]' => '{foo/}bar[/foo]', |
---|
30 | '[foo/]bar' => '{foo/}bar', |
---|
31 | '[foo]bar' => '{foo/}bar', |
---|
32 | '[foo attr]bar[/foo]' => '{foo attr}bar{/foo}', |
---|
33 | '[foo attr/]bar[/foo]' => '{foo attr/}bar[/foo]', |
---|
34 | '[foo attr/]bar' => '{foo attr/}bar', |
---|
35 | '[foo attr]bar' => '{foo attr/}bar', |
---|
36 | '[[foo]bar[/foo]' => '[{foo}bar{/foo}', |
---|
37 | '[[foo/]bar[/foo]' => '[{foo/}bar[/foo]', |
---|
38 | '[[foo/]bar' => '[{foo/}bar', |
---|
39 | '[[foo]bar' => '[{foo/}bar', |
---|
40 | '[[foo attr]bar[/foo]' => '[{foo attr}bar{/foo}', |
---|
41 | '[[foo attr/]bar[/foo]' => '[{foo attr/}bar[/foo]', |
---|
42 | '[[foo attr/]bar' => '[{foo attr/}bar', |
---|
43 | '[[foo attr]bar' => '[{foo attr/}bar', |
---|
44 | '[foo]bar[/foo]]' => '{foo}bar{/foo}]', |
---|
45 | '[foo/]]bar[/foo]' => '{foo/}]bar[/foo]', |
---|
46 | '[foo/]bar]' => '{foo/}bar]', |
---|
47 | '[foo]]bar' => '{foo/}]bar', |
---|
48 | '[foo attr]bar[/foo]]' => '{foo attr}bar{/foo}]', |
---|
49 | '[foo attr/]]bar[/foo]' => '{foo attr/}]bar[/foo]', |
---|
50 | '[foo attr/]bar]' => '{foo attr/}bar]', |
---|
51 | '[foo attr]]bar' => '{foo attr/}]bar', |
---|
52 | '[[foo]bar[/foo]]' => '[foo]bar[/foo]', |
---|
53 | '[[foo/]]bar[/foo]' => '[foo/]bar[/foo]', |
---|
54 | '[[foo/]]bar' => '[foo/]bar', |
---|
55 | '[[foo]]bar' => '[foo]bar', |
---|
56 | '[[foo attr]bar[/foo]]' => '[foo attr]bar[/foo]', |
---|
57 | '[[foo attr/]]bar[/foo]' => '[foo attr/]bar[/foo]', |
---|
58 | '[[foo attr/]]bar' => '[foo attr/]bar', |
---|
59 | '[[foo attr]]bar' => '[foo attr]bar', |
---|
60 | ); |
---|
61 | |
---|
62 | $extras = array_merge(array('' => '', "\n" => "\n", " " => " ", " \n " => " \n "), $tests); |
---|
63 | |
---|
64 | $regexes = array( |
---|
65 | '/(\[?)\[(foo)\b(.*?)(\/)?\](?(4)|(?:((?:(?!\[(?:foo)\b).)+?)\[\/\2\])?)?(\]?)/s' => 'Catch inner / using single inside parenthesis, self-closing shortcode, with look ahead', |
---|
66 | ); |
---|
67 | |
---|
68 | header('Content-Type: text/plain'); |
---|
69 | |
---|
70 | $i = 0; |
---|
71 | foreach ( $regexes as $pattern => $desc ) { |
---|
72 | $i++; |
---|
73 | $j = 0; |
---|
74 | echo "$i. $desc\n\n\"$pattern\"\n\n"; |
---|
75 | |
---|
76 | foreach ( $tests as $test => $test_return ) { |
---|
77 | foreach ( $extras as $extra => $extra_return ) { |
---|
78 | #echo "\n$test$extra:\n"; |
---|
79 | $return = preg_replace_callback($pattern, 'test_regexp', "$test$extra"); |
---|
80 | #echo "\n"; |
---|
81 | if ( $return == "$test_return$extra_return" ) { |
---|
82 | echo "1: $test$extra => $return\n"; |
---|
83 | $j++; |
---|
84 | } else { |
---|
85 | echo "0: $test$extra => $return vs. $test_return$extra_return\n"; |
---|
86 | } |
---|
87 | } |
---|
88 | } |
---|
89 | echo "\n"; |
---|
90 | echo "Total Score: $j"; |
---|
91 | echo "\n\n\n"; |
---|
92 | } |
---|
93 | die; |
---|
94 | ?> |
---|