如何匹配用户的输入'没有空格数组的元素与空白在PHP


How to match user's input without white space to array which has the element with white space in PHP?

假设我的数组[Table Tennis, Basketball, Swimming, ...]中有Table Tennis,用户输入是tabletennis,我如何使它匹配?

这是我目前拥有的,$q是用户输入,$skills是我的数组:

if (preg_grep("/$q/i", $skills)){
    $skill = implode(" ", preg_grep("/$q/i", $skills));
    $searchRows[$key]['skills'] = '<a href="javascript:void(0)" class="label label-info">' . $skill . '</a>';
}else{
    $searchRows[$key]['skills'] = count($skills) . ' skills';
}

提前感谢您的帮助

也许你可以用一个更简单的解决方案?

    <?php
$testArray = array("Table Tennis", "Cooking", "Swimming"); //array to test
$testtoArray = array("tabletennis", "cooking", "swi mming");//array to test against
foreach($testArray as $arrVal)
{
    $arrValExploded = explode(" ", $arrVal);
    $var = "";
    for($i=0;$i<=(count($arrValExploded)-1);$i++)
    {
        $var = $var . lcfirst($arrValExploded[$i]);
    }
    if(in_array($var,$testtoArray))
    {
        echo $arrVal . "<br>"; //using this you can get the value from $testArray
        for($i=0;$i<count($testtoArray);$i++)
        {
            if($var == $testtoArray[$i])
            {
                echo $var . "<br>";
            }
        }
    }
    else
    {
        echo "$arrVal didn't match <br>";
    }
}
?>

这可以适用于任何数量的空格。例如:——"这是一个游戏"匹配" thisisagame "。

希望strtolower ( string $string );str_replace(' ', '', $string);能帮到你。

例子:

$text = "Table Tennis s";    
$uinput = "tabletennis";    
$withoutwhitespace = str_replace(' ', '', $text);
$strarray = strtolower ($withoutwhitespace);
if($uinput == $strarray) {
  echo " Fine ";
} else {
  echo " Not Fine ";
}