removing @email.com from string in php

前端 未结 6 1701
感动是毒
感动是毒 2021-01-17 18:00

I want to just get the left half of an email address ( the username part of username@email.com ), so stripping the @ and any characters after it.

相关标签:
6条回答
  • 2021-01-17 18:14
    function subStrEmail($Useremail)
    {
        $emailSub=substr($Useremail,4);
        $email = explode('.', $emailSub);
    
        if($email[0]=='www'){
            $email=substr($emailSub,4);
            $email=ltrim($email,'www.');
            return $email;
        }
        return $emailSub;
    }
    
    0 讨论(0)
  • 2021-01-17 18:16

    you can split the string using explode()

    $email = 'hello@email.com';
    /*split the string bases on the @ position*/
    $parts = explode('@', $email);
    $namePart = $parts[0];
    
    0 讨论(0)
  • 2021-01-17 18:27

    Since nobody's used preg_match yet:

    <?php
        $email = 'user@email.com';
        preg_match('/(\S+)(@(\S+))/', $email, $match);
    
    /*  print_r($match);
            Array
            (
                [0] => user@email.com
                [1] => user
                [2] => @email.com
                [3] => email.com
            )
    */
    
        echo $match[1];  // output: `user`
    ?>
    

    Using an array means if you decide later that you want the email.com part, you've already got it separated out and don't have to drastically change your method. :)

    0 讨论(0)
  • 2021-01-17 18:27
    $text = 'abc@email.com';
    $text = str_replace('@email.com','',$text);
    
    0 讨论(0)
  • 2021-01-17 18:31

    If you have PHP5.3 you could use strstr

    $email = 'username@email.com';
    
    $username = strstr($email, '@', true); //"username"
    

    If not, just use the trusty substr

    $username = substr($email, 0, strpos($email, '@'));
    
    0 讨论(0)
  • 2021-01-17 18:36
    $parts=explode('@','username@email.com');
    
    echo $parts[0];// username
    echo $parts[1];// email.com
    
    0 讨论(0)
提交回复
热议问题