[ create a new paste ] login | about

Link: http://codepad.org/nUhFwKz1    [ raw code | output | fork ]

PHP, pasted on May 3:
<?php

function permutations($array)
{
 $list = array();
 for ($i=0; $i<=10000; $i++) {
  shuffle($array);
  $tmp = implode(',',$array);
  if (isset($list[$tmp])) {
   $list[$tmp]++;
  } else {
   $list[$tmp] = 1;
  }
 }
 ksort($list);
 $list = array_keys($list);
 return $list;
}



function CreateRegex($array)
{
	$toReturn = '/';
	foreach($array AS $value)
	{
		//Contains spaces
		if(strpos($value, " ") != false)
		{
			$pieces = explode(" ", $value);
			$combos = permutations($pieces);
			foreach($combos AS $currentCombo)
			{
				$currentPieces = explode(',', $currentCombo);
				foreach($currentPieces AS $finallyGotIt)
				{
					$toReturn .= '\b' . $finallyGotIt . '.*?';
				}
				$toReturn = substr($toReturn, 0, -3) . '|';
			}
		}
		else
		{
			$toReturn .= '\b' . $value . '|';
		}
	}

	$toReturn = substr($toReturn, 0, -1) . '/';
	return $toReturn;
}




var_dump(CreateRegex(array('apple', 'banana', 'strawberry', 'pear cake')));

?>


Output:
1
string(63) "/\bapple|\bbanana|\bstrawberry|\bcake.*?\bpear|\bpear.*?\bcake/"


Create a new paste based on this one


Comments: