php live search email address after @

99 views Asked by At
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);
    }

when i search if input is match with the email in the datase, I ONLY want to check everything before @ in the email address ignore everything after @. how am i able to do that?

i try to put

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

before display_function. it will remove everything after @ in the result. but it still display the wrong result. which the input string matching email part after @

3

There are 3 answers

0
ishwar On BEST ANSWER

Use this code:

$email  = '[email protected]';$user = explode('@', $email);echo $user[0];//prints username
0
Shankar Narayana Damodaran On

Make use of strstr() in PHP

<?php
$email  = '[email protected]';
$user = strstr($email, '@', true);
echo $user;//prints name

Linking your example...

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

Try this

$result = preg_split("/(@)/", $result['email']);
echo $result[0];