How to get a substring from string through PHP?

后端 未结 6 2210
渐次进展
渐次进展 2020-12-31 16:28

Hi want to change the displayed username like abcd@somedomain.com to only abcd. so for this i should clip the part starting from @.

I can do this very easily through

相关标签:
6条回答
  • 2020-12-31 16:57

    Try this:

    $username = substr($username, 0, strpos($username, '@'));
    
    0 讨论(0)
  • 2020-12-31 17:10

    Use strstr function.

    An example from the PHP reference -

    <?php
        $email  = 'name@example.com';
        $domain = strstr($email, '@');
        echo $domain; // prints @example.com
    
        $user = strstr($email, '@', true); // As of PHP 5.3.0
        echo $user; // prints name
    ?>
    
    0 讨论(0)
  • 2020-12-31 17:11
    $crop_username = substr($username, 0, strpos($username, '@'));
    

    This will help you

    0 讨论(0)
  • 2020-12-31 17:12
    substr(string, 0, 20)
    

    String, start, length

    0 讨论(0)
  • 2020-12-31 17:14
    list($username, $domain) = explode('@', 'asdf@somedomain.com')
    
    0 讨论(0)
  • 2020-12-31 17:19

    Use strtok().

    $username = strtok($email, '@');
    

    CodePad.

    0 讨论(0)
提交回复
热议问题