PHP 实时搜索电子邮件地址在 @ 之后


php live search email address after @

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['email']);
        $display_name = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['email']);
        //$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);
    }

当我搜索输入是否与数据库中的电子邮件匹配时,我只想检查电子邮件地址中@之前的所有内容,忽略@之后的所有内容。 我怎么能做到这一点?

我试着把

$result = array_shift(explode('@', $result['email']));

在display_function之前。 它将删除结果中@之后的所有内容。 但它仍然显示错误的结果。 其中输入字符串匹配@之后的电子邮件部分

使用以下代码:

$email  = 'username@example.com';$user = explode('@', $email);echo $user[0];//prints username

在 PHP 中使用strstr()

<?php
$email  = 'name@example.com';
$user = strstr($email, '@', true);
echo $user;//prints name

链接您的示例...

$result = strstr($result['email'],'@',true);

试试这个

$result = preg_split("/(@)/", $result['email']);
echo $result[0];
相关文章: