PageRenderTime 32ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/hphp/test/slow/ext_preg/ext_preg.php

http://github.com/facebook/hiphop-php
PHP | 384 lines | 327 code | 44 blank | 13 comment | 3 complexity | 38f2cdf0646d8c32aa61c50c255103d4 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
  1. <?hh
  2. function VS($x, $y) {
  3. var_dump($x === $y);
  4. if ($x !== $y) { echo "Failed: $y\n"; var_dump(debug_backtrace()); }
  5. }
  6. function VERIFY($x) { VS($x, true); }
  7. //////////////////////////////////////////////////////////////////////
  8. function test_preg_rep($a,$b,$c) {
  9. return strtoupper($c).$a;
  10. }
  11. function test_preg_grep() {
  12. $array = varray["foo 123.1", "fg 24bar", "123.1", "24"];
  13. $fl_array = preg_grep("/^(\\d+)?\\.\\d+$/", $array);
  14. VS(count($fl_array), 1);
  15. VS($fl_array[2], "123.1");
  16. VS(preg_grep("/a/", varray["c", "b"]), varray[]);
  17. }
  18. function test_preg_match() {
  19. // The "i" after the pattern delimiter indicates a case-insensitive search
  20. VS(preg_match("/php/i", "PHP is a scripting language."), 1);
  21. // The \b in the pattern indicates a word boundary, so only the distinct
  22. // word "web" is matched, and not a word partial like "webbing" or "cobweb"
  23. VS(preg_match("/\\bweb\\b/i", "is the web scripting"), 1);
  24. // get host name from URL
  25. $matches = null;
  26. preg_match_with_matches(
  27. "@^(?:http://)?([^/]+)@i",
  28. "http://www.php.net/index.html",
  29. inout $matches,
  30. );
  31. $host = $matches[1];
  32. VS($host, "www.php.net");
  33. // get last two segments of host name
  34. preg_match_with_matches("/[^.]+\\.[^.]+$/", $host, inout $matches);
  35. VS($matches[0], "php.net");
  36. $str = "foobar: 2008";
  37. preg_match_with_matches("/(?<name>\\w+): (?<digit>\\d+)/", $str, inout $matches);
  38. VS(print_r($matches, true),
  39. "Array\n".
  40. "(\n".
  41. " [0] => foobar: 2008\n".
  42. " [name] => foobar\n".
  43. " [1] => foobar\n".
  44. " [digit] => 2008\n".
  45. " [2] => 2008\n".
  46. ")\n");
  47. }
  48. function test_preg_match_all() {
  49. $matches = null;
  50. preg_match_all_with_matches(
  51. "/\\(? (\\d{3})? \\)? (?(1) [\\-\\s] ) \\d{3}-\\d{4}/x",
  52. "Call 555-1212 or 1-800-555-1212",
  53. inout $matches,
  54. );
  55. VS(print_r($matches, true),
  56. "Array\n".
  57. "(\n".
  58. " [0] => Array\n".
  59. " (\n".
  60. " [0] => 555-1212\n".
  61. " [1] => 800-555-1212\n".
  62. " )\n".
  63. "\n".
  64. " [1] => Array\n".
  65. " (\n".
  66. " [0] => \n".
  67. " [1] => 800\n".
  68. " )\n".
  69. "\n".
  70. ")\n");
  71. // The \\2 is an example of backreferencing. This tells pcre that
  72. // it must match the second set of parentheses in the regular expression
  73. // itself, which would be the ([\w]+) in this case. The extra backslash is
  74. // required because the string is in double quotes.
  75. $html = "<b>bold text</b><a href=howdy.html>click me</a>";
  76. preg_match_all_with_matches(
  77. "/(<([\\w]+)[^>]*>)(.*)(<\\/\\2>)/",
  78. $html,
  79. inout $matches,
  80. PREG_SET_ORDER,
  81. );
  82. VS(print_r($matches, true),
  83. "Array\n".
  84. "(\n".
  85. " [0] => Array\n".
  86. " (\n".
  87. " [0] => <b>bold text</b>\n".
  88. " [1] => <b>\n".
  89. " [2] => b\n".
  90. " [3] => bold text\n".
  91. " [4] => </b>\n".
  92. " )\n".
  93. "\n".
  94. " [1] => Array\n".
  95. " (\n".
  96. " [0] => <a href=howdy.html>click me</a>\n".
  97. " [1] => <a href=howdy.html>\n".
  98. " [2] => a\n".
  99. " [3] => click me\n".
  100. " [4] => </a>\n".
  101. " )\n".
  102. "\n".
  103. ")\n");
  104. $str = "a: 1\nb: 2\nc: 3\n";
  105. preg_match_all_with_matches(
  106. "/(?<name>\\w+): (?<digit>\\d+)/",
  107. $str,
  108. inout $matches,
  109. );
  110. VS(print_r($matches, true),
  111. "Array\n".
  112. "(\n".
  113. " [0] => Array\n".
  114. " (\n".
  115. " [0] => a: 1\n".
  116. " [1] => b: 2\n".
  117. " [2] => c: 3\n".
  118. " )\n".
  119. "\n".
  120. " [name] => Array\n".
  121. " (\n".
  122. " [0] => a\n".
  123. " [1] => b\n".
  124. " [2] => c\n".
  125. " )\n".
  126. "\n".
  127. " [1] => Array\n".
  128. " (\n".
  129. " [0] => a\n".
  130. " [1] => b\n".
  131. " [2] => c\n".
  132. " )\n".
  133. "\n".
  134. " [digit] => Array\n".
  135. " (\n".
  136. " [0] => 1\n".
  137. " [1] => 2\n".
  138. " [2] => 3\n".
  139. " )\n".
  140. "\n".
  141. " [2] => Array\n".
  142. " (\n".
  143. " [0] => 1\n".
  144. " [1] => 2\n".
  145. " [2] => 3\n".
  146. " )\n".
  147. "\n".
  148. ")\n");
  149. }
  150. function test_preg_replace() {
  151. $str = "April 15, 2003";
  152. $pattern = "/(\\w+) (\\d+), (\\d+)/i";
  153. $replacement = "\${1}1,\$3";
  154. VS(preg_replace($pattern, $replacement, $str), "April1,2003");
  155. $str = "The quick brown fox jumped over the lazy dog.";
  156. $patterns = darray[];
  157. $replacements = darray[];
  158. $patterns[0] = "/quick/";
  159. $patterns[1] = "/brown/";
  160. $patterns[2] = "/fox/";
  161. $replacements[2] = "bear";
  162. $replacements[1] = "black";
  163. $replacements[0] = "slow";
  164. VS(preg_replace($patterns, $replacements, $str),
  165. "The bear black slow jumped over the lazy dog.");
  166. ksort(inout $patterns);
  167. ksort(inout $replacements);
  168. VS(preg_replace($patterns, $replacements, $str),
  169. "The slow black bear jumped over the lazy dog.");
  170. $foos = darray[];
  171. $foos[0] = "foo";
  172. $foos[1] = "Foo";
  173. $foos[2] = "FOO";
  174. $expFoo = darray[];
  175. $expFoo[0] = "FOO";
  176. $expFoo[1] = "FOO";
  177. $expFoo[2] = "FOO";
  178. VS(preg_replace("/some pattern/", "", varray[]), varray[]);
  179. VS(preg_replace("/foo/i", "FOO", $foos), $expFoo);
  180. $patterns = varray["/(19|20)(\\d{2})-(\\d{1,2})-(\\d{1,2})/",
  181. "/^\\s*{(\\w+)}\\s*=/"];
  182. $replace = varray["\\3/\\4/\\1\\2", "$\\1 ="];
  183. VS(preg_replace($patterns, $replace, "{startDate} = 1999-5-27"),
  184. "\$startDate = 5/27/1999");
  185. $str = "foo o";
  186. $str = preg_replace("/\\s\\s+/", " ", $str);
  187. VS($str, "foo o");
  188. $count = 0;
  189. preg_replace_with_count(varray["/\\d/", "/\\s/"], "*", "xp 4 to", -1, inout $count);
  190. VS($count, 3);
  191. VS(preg_replace("/xxx", "w", "xxxx"), NULL);
  192. VS(preg_replace("/xxx/", "w", "xxxx"), "wx");
  193. VS(preg_replace("/xxy/", "w", "xxxx"), "xxxx");
  194. VS(preg_replace("/xxx", "w", varray["xxxx"]), varray[]);
  195. VS(preg_replace("/xxx/", "w", varray["xxxx"]), varray["wx"]);
  196. VS(preg_replace("/xxx/", "w", varray["xxxx", "yyyy"]), varray["wx", "yyyy"]);
  197. VS(preg_replace(varray["/xxx/", "/xxx"], "w", varray["xxxx"]), varray[]);
  198. VS(preg_replace(varray["/xxx/", "/xxx/"], "w", varray["xxxx"]), varray["wx"]);
  199. VS(preg_replace("/xxx", varray["w"], varray["xxxx"]), false);
  200. VS(preg_replace(varray["/xxx"], varray["w"], varray["xxxx"]), varray[]);
  201. VS(preg_replace(varray["/xxx/"], varray["w"], varray["xxxx"]), varray["wx"]);
  202. }
  203. function next_year($m) {
  204. return $m[1].((int)$m[2] + 1);
  205. }
  206. function test_preg_replace_callback() {
  207. $text = "April fools day is 04/01/2002\n".
  208. "Last christmas was 12/24/2001\n";
  209. $count = -1;
  210. $text = preg_replace_callback("|(\\d{2}/\\d{2}/)(\\d{4})|", fun("next_year"),
  211. $text, -1, inout $count);
  212. VS($text, "April fools day is 04/01/2003\nLast christmas was 12/24/2002\n");
  213. }
  214. function test_preg_split() {
  215. $keywords = preg_split("/[\\s,]+/",
  216. "hypertext language, programming");
  217. VS(count($keywords), 3);
  218. VS($keywords[0], "hypertext");
  219. VS($keywords[1], "language");
  220. VS($keywords[2], "programming");
  221. $str = "string";
  222. $chars = preg_split("//", $str, -1, PREG_SPLIT_NO_EMPTY);
  223. VS(count($chars), 6);
  224. VS($chars[0], "s");
  225. VS($chars[1], "t");
  226. VS($chars[2], "r");
  227. VS($chars[3], "i");
  228. VS($chars[4], "n");
  229. VS($chars[5], "g");
  230. $chars = preg_split("//", $str, null, PREG_SPLIT_NO_EMPTY);
  231. VS(count($chars), 6);
  232. VS($chars[0], "s");
  233. VS($chars[1], "t");
  234. VS($chars[2], "r");
  235. VS($chars[3], "i");
  236. VS($chars[4], "n");
  237. VS($chars[5], "g");
  238. $chars = preg_split("//", $str, 3, PREG_SPLIT_NO_EMPTY);
  239. VS(count($chars), 3);
  240. VS($chars[0], "s");
  241. VS($chars[1], "t");
  242. VS($chars[2], "ring");
  243. $str = "hypertext language programming";
  244. $chars = preg_split("/ /", $str, -1, PREG_SPLIT_OFFSET_CAPTURE);
  245. VS(print_r($chars, true),
  246. "Array\n".
  247. "(\n".
  248. " [0] => Array\n".
  249. " (\n".
  250. " [0] => hypertext\n".
  251. " [1] => 0\n".
  252. " )\n".
  253. "\n".
  254. " [1] => Array\n".
  255. " (\n".
  256. " [0] => language\n".
  257. " [1] => 10\n".
  258. " )\n".
  259. "\n".
  260. " [2] => Array\n".
  261. " (\n".
  262. " [0] => programming\n".
  263. " [1] => 19\n".
  264. " )\n".
  265. "\n".
  266. ")\n");
  267. }
  268. function test_preg_quote() {
  269. $keywords = "$40 for a g3/400";
  270. $keywords = preg_quote($keywords, "/");
  271. VS($keywords, "\\$40 for a g3\\/400");
  272. // In this example, preg_quote($word) is used to keep the
  273. // asterisks from having special meaning to the regular
  274. // expression.
  275. $textbody = "This book is *very* difficult to find.";
  276. $word = "*very*";
  277. $textbody = preg_replace("/".preg_quote($word)."/",
  278. "<i>". $word ."</i>",
  279. $textbody);
  280. VS($textbody, "This book is <i>*very*</i> difficult to find.");
  281. }
  282. function test_ereg_replace() {
  283. $str = "This is a test";
  284. VS(str_replace(" is", " was", $str), "This was a test");
  285. VS(ereg_replace("( )is", "\\1was", $str), "This was a test");
  286. VS(ereg_replace("(( )is)", "\\2was", $str), "This was a test");
  287. $num = '4';
  288. $str = "This string has four words.";
  289. $str = ereg_replace("four", $num, $str);
  290. VS($str, "This string has 4 words.");
  291. $test = "http://test.com/test";
  292. $test = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
  293. "<a href=\"\\0\">\\0</a>", $test);
  294. VS($test, "<a href=\"http://test.com/test\">http://test.com/test</a>");
  295. }
  296. function test_eregi_replace() {
  297. $pattern = "(>[^<]*)(suffix)";
  298. $replacement = "\\1<span class=\"search\">\\2</span>";
  299. $body = ">whateversuffix";
  300. $body = eregi_replace($pattern, $replacement, $body);
  301. VS($body, ">whatever<span class=\"search\">suffix</span>");
  302. }
  303. function test_split() {
  304. $mb = "\xe4\xbf\xa1\xe6\x81\xaf\x01 2366797";
  305. $ret = split("\x01", $mb);
  306. VS($ret[0], "\xe4\xbf\xa1\xe6\x81\xaf");
  307. VS($ret[1], " 2366797");
  308. $date = "04/30/1973";
  309. $ret = split("[/.-]", $date);
  310. VS($ret[0], "04");
  311. VS($ret[1], "30");
  312. VS($ret[2], "1973");
  313. }
  314. function test_spliti() {
  315. $str = "aBBBaCCCADDDaEEEaGGGA";
  316. $chunks = spliti("a", $str, 5);
  317. VS($chunks[0], "");
  318. VS($chunks[1], "BBB");
  319. VS($chunks[2], "CCC");
  320. VS($chunks[3], "DDD");
  321. VS($chunks[4], "EEEaGGGA");
  322. }
  323. function test_sql_regcase() {
  324. VS(sql_regcase("Foo - bar."), "[Ff][Oo][Oo] - [Bb][Aa][Rr].");
  325. }
  326. <<__EntryPoint>>
  327. function main_ext_preg() {
  328. test_preg_grep();
  329. test_preg_match();
  330. test_preg_match_all();
  331. test_preg_replace();
  332. test_preg_replace_callback();
  333. test_preg_split();
  334. test_preg_quote();
  335. test_ereg_replace();
  336. test_eregi_replace();
  337. test_split();
  338. test_spliti();
  339. test_sql_regcase();
  340. }