| 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)|(?:(.+?)\[\/\2\])?)?(.?)/s' => 'Catch inner / single outside parenthesis, self-closing shortcode, using dots for shortcode escaping',
|
|---|
| 66 | '/(.?)\[(foo)\b(.*?)(\/)?\](?(4)|(?:(.+?)\[\/\2\])?)?(.?)/s' => 'Catch inner / using single inside parenthesis, self-closing shortcode, using dots for shortcode escaping',
|
|---|
| 67 | '/(.?)\[(foo)\b(.*?)(?:(\/))?\](?(4)|(?:(.+?)\[\/\2\])?)?(.?)/s' => 'Catch inner / using double parenthesis, self-closing shortcode, using dots for shortcode escaping',
|
|---|
| 68 |
|
|---|
| 69 | '/(.?)\[(foo)\b(.*?)(\/?)\](?:(.+?)\[\/\2\])?(.?)/s' => 'Catch inner / using single outside parenthesis, no self-closing shortcode, using dots for shortcode escaping',
|
|---|
| 70 | '/(.?)\[(foo)\b(.*?)(\/)?\](?:(.+?)\[\/\2\])?(.?)/s' => 'Catch inner / using single inside parenthesis, no self-closing shortcode, using dots for shortcode escaping',
|
|---|
| 71 | '/(.?)\[(foo)\b(.*?)(?:(\/))?\](?:(.+?)\[\/\2\])?(.?)/s' => 'Catch inner / using double parenthesis, no self-closing shortcode, using dots for shortcode escaping (current WP regexp)',
|
|---|
| 72 |
|
|---|
| 73 | '/(.?)\[(foo)\b(.*?)(\/?)\](?(4)|(?:((?:(?!\[(?:foo)\b).)+?)\[\/\2\])?)?(.?)/s' => 'Catch inner / single outside parenthesis, self-closing shortcode, with look ahead, using dots for shortcode escaping',
|
|---|
| 74 | '/(.?)\[(foo)\b(.*?)(\/)?\](?(4)|(?:((?:(?!\[(?:foo)\b).)+?)\[\/\2\])?)?(.?)/s' => 'Catch inner / using single inside parenthesis, self-closing shortcode, with look ahead, using dots for shortcode escaping',
|
|---|
| 75 | '/(.?)\[(foo)\b(.*?)(?:(\/))?\](?(4)|(?:((?:(?!\[(?:foo)\b).)+?)\[\/\2\])?)?(.?)/s' => 'Catch inner / using double parenthesis, self-closing shortcode, with look ahead, using dots for shortcode escaping',
|
|---|
| 76 |
|
|---|
| 77 | '/(.?)\[(foo)\b(.*?)(\/?)\](?:((?:(?!\[(?:foo)\b).)+?)\[\/\2\])?(.?)/s' => 'Catch inner / using single outside parenthesis, no self-closing shortcode, with look ahead, using dots for shortcode escaping',
|
|---|
| 78 | '/(.?)\[(foo)\b(.*?)(\/)?\](?:((?:(?!\[(?:foo)\b).)+?)\[\/\2\])?(.?)/s' => 'Catch inner / using single inside parenthesis, no self-closing shortcode, with look ahead, using dots for shortcode escaping',
|
|---|
| 79 | '/(.?)\[(foo)\b(.*?)(?:(\/))?\](?:((?:(?!\[(?:foo)\b).)+?)\[\/\2\])?(.?)/s' => 'Catch inner / using double parenthesis, no self-closing shortcode, with look ahead, using dots for shortcode escaping',
|
|---|
| 80 |
|
|---|
| 81 | '/(\[?)\[(foo)\b(.*?)(\/?)\](?(4)|(?:(.+?)\[\/\2\])?)?(\]?)/s' => 'Catch inner / single outside parenthesis, self-closing shortcode',
|
|---|
| 82 | '/(\[?)\[(foo)\b(.*?)(\/)?\](?(4)|(?:(.+?)\[\/\2\])?)?(\]?)/s' => 'Catch inner / using single inside parenthesis, self-closing shortcode',
|
|---|
| 83 | '/(\[?)\[(foo)\b(.*?)(?:(\/))?\](?(4)|(?:(.+?)\[\/\2\])?)?(\]?)/s' => 'Catch inner / using double parenthesis, self-closing shortcode',
|
|---|
| 84 |
|
|---|
| 85 | '/(\[?)\[(foo)\b(.*?)(\/?)\](?:(.+?)\[\/\2\])?(\]?)/s' => 'Catch inner / using single outside parenthesis, no self-closing shortcode',
|
|---|
| 86 | '/(\[?)\[(foo)\b(.*?)(\/)?\](?:(.+?)\[\/\2\])?(\]?)/s' => 'Catch inner / using single inside parenthesis, no self-closing shortcode',
|
|---|
| 87 | '/(\[?)\[(foo)\b(.*?)(?:(\/))?\](?:(.+?)\[\/\2\])?(\]?)/s' => 'Catch inner / using double parenthesis, no self-closing shortcode',
|
|---|
| 88 |
|
|---|
| 89 | '/(\[?)\[(foo)\b(.*?)(\/?)\](?(4)|(?:((?:(?!\[(?:foo)\b).)+?)\[\/\2\])?)?(\]?)/s' => 'Catch inner / single outside parenthesis, self-closing shortcode, with look ahead',
|
|---|
| 90 | '/(\[?)\[(foo)\b(.*?)(\/)?\](?(4)|(?:((?:(?!\[(?:foo)\b).)+?)\[\/\2\])?)?(\]?)/s' => 'Catch inner / using single inside parenthesis, self-closing shortcode, with look ahead',
|
|---|
| 91 | '/(\[?)\[(foo)\b(.*?)(?:(\/))?\](?(4)|(?:((?:(?!\[(?:foo)\b).)+?)\[\/\2\])?)?(\]?)/s' => 'Catch inner / using double parenthesis, self-closing shortcode, with look ahead',
|
|---|
| 92 |
|
|---|
| 93 | '/(\[?)\[(foo)\b(.*?)(\/?)\](?:((?:(?!\[(?:foo)\b).)+?)\[\/\2\])?(\]?)/s' => 'Catch inner / using single outside parenthesis, no self-closing shortcode, with look ahead',
|
|---|
| 94 | '/(\[?)\[(foo)\b(.*?)(\/)?\](?:((?:(?!\[(?:foo)\b).)+?)\[\/\2\])?(\]?)/s' => 'Catch inner / using single inside parenthesis, no self-closing shortcode, with look ahead',
|
|---|
| 95 | '/(\[?)\[(foo)\b(.*?)(?:(\/))?\](?:((?:(?!\[(?:foo)\b).)+?)\[\/\2\])?(\]?)/s' => 'Catch inner / using double parenthesis, no self-closing shortcode, with look ahead',
|
|---|
| 96 | );
|
|---|
| 97 |
|
|---|
| 98 | header('Content-Type: text/plain');
|
|---|
| 99 |
|
|---|
| 100 | $i = 0;
|
|---|
| 101 | foreach ( $regexes as $pattern => $desc ) {
|
|---|
| 102 | $i++;
|
|---|
| 103 | $j = 0;
|
|---|
| 104 | $k = 0;
|
|---|
| 105 | echo "$i. $desc\n\n\"$pattern\"\n\n";
|
|---|
| 106 |
|
|---|
| 107 | foreach ( $tests as $test => $test_return ) {
|
|---|
| 108 | foreach ( $extras as $extra => $extra_return ) {
|
|---|
| 109 | $k++;
|
|---|
| 110 | #echo "\n$test$extra:\n";
|
|---|
| 111 | $return = preg_replace_callback($pattern, 'test_regexp', "$test$extra");
|
|---|
| 112 | #echo "\n";
|
|---|
| 113 | if ( $return == "$test_return$extra_return" ) {
|
|---|
| 114 | # echo "1: $test$extra => $return\n";
|
|---|
| 115 | $j++;
|
|---|
| 116 | } else {
|
|---|
| 117 | # echo "0: $test$extra => $return vs. $test_return$extra_return\n";
|
|---|
| 118 | }
|
|---|
| 119 | }
|
|---|
| 120 | }
|
|---|
| 121 | echo "\n";
|
|---|
| 122 | echo "Total Score: $j / $k";
|
|---|
| 123 | echo "\n\n\n";
|
|---|
| 124 | }
|
|---|
| 125 | die;
|
|---|
| 126 | ?>
|
|---|