Jump to content

selecting part of a string


jeff5656

Recommended Posts

Pick any one from this incomplete list:

list($username) = explode("@", $string);

$parts = explode("@", $string);
$username = $parts[0];

$username = strtok("@", $string);

$at = strpos($string, "@");
$username = substr($string, 0, $at);

preg_match('/[^@]+(?=@)/', $string, $matches);
$username = $matches[0];

$username = preg_replace('/@.*/', "", $string);

list($username) = sscanf($string, "%[^@]");

$len = strcspn($string, "@");
$username = substr($string, 0, $len);

$username = strstr($string, "@", true);

I'm partial to #3.

I ended up using substr and strpos

Mind posting the solution so others can reference?

But you didn't post your solution for "explode" so others can reference it! :-):-)

Anyway mine was :

$user = substr($email, 0, strpos($email, "@"));

I didn't realize there were so many ways to skin this cat...Good future reference

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.