0
if (strlen($search_string) >= 1 && $search_string !== ' ') {
// Build Query
$query = 'SELECT * FROM User WHERE email LIKE "%'.$search_string.'%"';

// Do Search
$result = $tutorial_db->query($query);
while($results = $result->fetch_array()) {
    $result_array[] = $results;
}

// Check If We Have Results
if (isset($result_array)) {
    foreach ($result_array as $result) {

        // Format Output Strings And Hightlight Matches
        $display_function = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['firstName']);
        $display_name = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['lastName']);
        //$display_url = 'http://php.net/manual-lookup.php?pattern='.urlencode($result['function']).'&lang=en';

        // Insert Name
        $output = str_replace('nameString', $display_name, $html);

        // Insert Function
        $output = str_replace('functionString', $display_function, $output);

        // Insert URL
        //$output = str_replace('urlString', $display_url, $output);

        // Output
        echo($output);
    }
}else{

    // Format No Results Output
    $output = str_replace('urlString', 'javascript:void(0);', $html);
    $output = str_replace('nameString', '<b>No Results Found.</b>', $output);
    $output = str_replace('functionString', 'Sorry :(', $output);

    // Output
    echo($output);
}
}

这是一个实时搜索功能。搜索电子邮件地址,并显示匹配的名字和姓氏。我有两个问题。

1:当我输入@时它不会给我任何结果。

2:我希望我可以点击结果并输入输入框,但现在它只是重新打开页面。

3:我只想搜索@之前的邮箱地址。比如 text@hotmail.com。我只需要搜索文本而不是 hotmail.com。我怎么能做到这一点。电子邮件在数据库中

4

1 回答 1

0

@在输入框中输入时出现此部分的问题

    foreach ($result_array as $result) {

    // Make Sure you have @ in your $result['firstName']
    $display_function = preg_replace( 
                               "/".$search_string."/i", 
                               "<b class='highlight'>".$search_string."</b>", 
                               $result['firstName']);

    // Make Sure you have @ in your $result['lastName']
    $display_name = preg_replace(
                               "/".$search_string."/i", 
                               "<b class='highlight'>".$search_string."</b>", 
                               $result['lastName']);



// Insert Name
$output = str_replace('nameString', $display_name, $html);

// Insert Function
$output = str_replace('functionString', $display_function, $output);

// Insert URL
        //$output = str_replace('urlString', $display_url, $output);

        // Output
        echo($output);
    }

preg_replace() 如果主题参数是一个数组,则返回一个数组,否则返回一个字符串。

看看preg_replace()函数

 If matches are found, the new subject will be returned, otherwise subject  
 will be returned unchanged or NULL if an error occurred. 
于 2013-12-05T04:19:34.207 回答