PHP regex generator

2019-03-06 00:37发布

问题:

I have now got a working regex string for the below needed criteria:

a one line php-ready regex that encompasses a number of keywords, and keyterms and will match at least one of them.

For example:

Keyterms:

  • apple
  • banana
  • strawberry
  • pear cake

Now if any of these key terms are found then it returns true. However, to add a little more difficulty here, the pear cake term should be split as two keywords which must both be in the string, but need not be together.

Example strings which should return true:

  • A great cake is made from pear
  • i like apples
  • i like apples and bananas
  • i like cakes made from pear and apples
  • I like cakes made from pears

The working regex is:

/\bapple|\bbanana|\bstrawberry|\bpear.*?\bcake|\bcake.*?\bpear/

Now I need a php function that will create this regex on the fly from an array of keyterms. The stickler is that a keyterm may have any number of keywords within that key. Only on of the keyterms need be found, but multiple can be present. As above all of the the words within a keyterm must appear in the string in any order.

回答1:

I have written a function for you here:

<?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')));

?>

I got the permutations function from:

http://www.hashbangcode.com/blog/getting-all-permutations-array-php-74.html

But I would recommend to find a better function and use another one since just at first glance this one is pretty ugly since it increments $i to 10,000 no matter what.

Also, here is a codepad for the code:

http://codepad.org/nUhFwKz1

Let me know if there is something wrong with it!