-1

这是我试图让搜索引擎获取输入为 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 ]/以及其他一些不成功的变体。

4

1 回答 1

2
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

于 2012-05-23T16:00:23.893 回答