非字母数字字符替换为单个空格


Non alphanumeric characters replaced with a single space

这就是

我试图让搜索引擎选择输入为Doe J而不是Doe,J的名字。

这是我的代码:

$sql_where = array();
if (isset($_GET['name'])) {
    echo "Searched: {$_GET['name']}<br>";
    $names = explode(' ', trim(preg_replace('/ +/', ' ', $_GET['name'])));
    $names_cnt = count($names);
    if (2 == $names_cnt) {
        foreach ($names as $name_idx => $name) {
            if (($name_idx+1) == $names_cnt) {
                // last one
                $sql_where[] = "
                    (full_name like '% {$name}%')
                    ";
            } else {
                // first one
                $sql_where[] = "
                    (full_name like '{$name}%')
                    ";
            }
        }
    } else {
        $sql_where[] = "
            (full_name like '" . $DB->cleanString($_GET['name']) . "%')
            ";

我尝试了/[^a-zA-Z0-9 ]/和其他一些不成功的变体。

echo preg_replace( "`[^a-zA-Z0-9]+`", " ", $string); 
//replaces all non alpha numeric characters as " " 
//(and will not have duplicate spaces)

演示:http://codepad.org/7Ltx3kcr