Guest User

Untitled

a guest
Jul 16th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. /bapple|bbanana|bstrawberry|bpear.*?bcake|bcake.*?bpear/
  2.  
  3. <?php
  4.  
  5. function permutations($array)
  6. {
  7. $list = array();
  8. for ($i=0; $i<=10000; $i++) {
  9. shuffle($array);
  10. $tmp = implode(',',$array);
  11. if (isset($list[$tmp])) {
  12. $list[$tmp]++;
  13. } else {
  14. $list[$tmp] = 1;
  15. }
  16. }
  17. ksort($list);
  18. $list = array_keys($list);
  19. return $list;
  20. }
  21.  
  22.  
  23.  
  24. function CreateRegex($array)
  25. {
  26. $toReturn = '/';
  27. foreach($array AS $value)
  28. {
  29. //Contains spaces
  30. if(strpos($value, " ") != false)
  31. {
  32. $pieces = explode(" ", $value);
  33. $combos = permutations($pieces);
  34. foreach($combos AS $currentCombo)
  35. {
  36. $currentPieces = explode(',', $currentCombo);
  37. foreach($currentPieces AS $finallyGotIt)
  38. {
  39. $toReturn .= 'b' . $finallyGotIt . '.*?';
  40. }
  41. $toReturn = substr($toReturn, 0, -3) . '|';
  42. }
  43. }
  44. else
  45. {
  46. $toReturn .= 'b' . $value . '|';
  47. }
  48. }
  49.  
  50. $toReturn = substr($toReturn, 0, -1) . '/';
  51. return $toReturn;
  52. }
  53.  
  54.  
  55.  
  56.  
  57. var_dump(CreateRegex(array('apple', 'banana', 'strawberry', 'pear cake')));
  58.  
  59. ?>
Add Comment
Please, Sign In to add comment