3

I'm very new to regular expressions and can't really figure out how it works. I have tried this:

function change_email($email){
   return preg_match('/^[\w]$/', $email);
}

But this only returns a boolean true or false, I want it to return everything BEFORE the @. Is this possible? I don't even think I use the right php function here..

Martezz
  • 35
  • 7
  • 1
    an `explode` or `substr` would be better fit here. If you are interested to understand the [preg_match](http://www.php.net/manual/en/function.preg-match.php) function, php has got one of the [best documentations](http://www.php.net/manual/en/function.preg-match.php) in the world. See the sample codes. Matches are available if you use a 3rd parameter in the `preg_match` call. – Mithun Satheesh Jun 30 '14 at 14:16
  • E-mail addresses can be more complicated. While it's unlikely you will encounter one in the wild, remember that an e-mail address may contain more than one `@` symbol. http://stackoverflow.com/a/12355897/362536 – Brad Jun 30 '14 at 14:41
  • @user3465900 Please accept the answer which helped you. Don't leave answers unaccepted just like that if you got answer to your question. – Abhineet Verma Jun 30 '14 at 15:00

6 Answers6

6

Try a much easier way with explode:

explode('@', $email)[0];
ajp15243
  • 7,704
  • 1
  • 32
  • 38
Lewis
  • 14,132
  • 12
  • 66
  • 87
3

Use strpos to get the position of the @ character and substr to crop out the email:

function change_email($email){
    return substr($email, 0, strpos($email, '@'));
}

Example:

<?php

function change_email($email){
    return substr($email, 0, strpos($email, '@'));
}

var_dump( change_email( 'foo@bar.com' )); // string(3) "foo"
var_dump( change_email( 'example.here@domain.net' )); // string(12) "example.here"
var_dump( change_email( 'not.an.email' )); // string(0) ""

DEMO

h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
2

What you want to use is strstr() function which you can read about here

$email = "name@email.com"
$user = strstr($email, '@', true); // As of PHP 5.3.0
echo $user; // prints name
Takoyaro
  • 928
  • 7
  • 14
2

RegEx

.*(?=@)

Demo

$re = "/.*(?=@)/"; 
$str = "example@something.com"; 

preg_match($re, $str, $matches);
Harpreet Singh
  • 2,651
  • 21
  • 31
2

You have a 3rd argument in preg_match which holds the matched items.

Eg:

preg_match( '/(?P<email_name>[a-zA-Z0-9._]+)@(?P<email_type>\w+)\.\w{2,4}/', $email, $matches );

If $email = 'hello@gmail.com'
$matches['email_name'] will be equal to "hello"
$mathces['email_type'] will be equal to "gmail"

Note that email name may only contains letters, numbers, underscore and dot. If you want to add some extra characters add them in the character class --> [a-zA-Z0-9._ other characters ]

DrGeneral
  • 1,844
  • 1
  • 16
  • 22
  • Real e-mail addresses can contain all sorts of stuff... even spaces if within quotes! – Brad Jun 30 '14 at 14:40
  • @Brad as I said, you can add other characters if you want. If you want to allow space you can use [a-zA-Z0-9._\s]. Anyway i dont know any email address with space in it. :) – DrGeneral Jun 30 '14 at 14:50
  • Agreed, I'm just throwing the caution out there. I try to use the Gmail feature of `user+whateveryouwant@gmail.com` all the time, and it often fails because someone's regex is not complete. – Brad Jun 30 '14 at 15:09
0

Through regex,

<?php
$mystring = "foo@bar.com";
$regex = '~^([^@]*)~';
if (preg_match($regex, $mystring, $m)) {
    $yourmatch = $m[1]; 
    echo $yourmatch;
    }
?> //=> foo
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274