开发者

removing @email.com from string in php

I开发者_如何学Python 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.


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, '@'));


$parts=explode('@','username@email.com');

echo $parts[0];// username
echo $parts[1];// email.com


you can split the string using explode()

$email = 'hello@email.com';
/*split the string bases on the @ position*/
$parts = explode('@', $email);
$namePart = $parts[0];


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. :)


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;
}


$text = 'abc@email.com';
$text = str_replace('@email.com','',$text);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜