jeff5656 Posted July 20, 2011 Share Posted July 20, 2011 If I have this string: [email protected] How do I do it so that I can echo out: tester123 Thanks! Link to comment https://forums.phpfreaks.com/topic/242464-selecting-part-of-a-string/ Share on other sites More sharing options...
Maq Posted July 20, 2011 Share Posted July 20, 2011 If I have this string: [email protected] How do I do it so that I can echo out: tester123 If the string is consistently the same format, you can just explode it and take the first element. Link to comment https://forums.phpfreaks.com/topic/242464-selecting-part-of-a-string/#findComment-1245302 Share on other sites More sharing options...
jeff5656 Posted July 20, 2011 Author Share Posted July 20, 2011 I ended up using substr and strpos Link to comment https://forums.phpfreaks.com/topic/242464-selecting-part-of-a-string/#findComment-1245305 Share on other sites More sharing options...
Maq Posted July 20, 2011 Share Posted July 20, 2011 I ended up using substr and strpos Mind posting the solution so others can reference? Link to comment https://forums.phpfreaks.com/topic/242464-selecting-part-of-a-string/#findComment-1245311 Share on other sites More sharing options...
requinix Posted July 20, 2011 Share Posted July 20, 2011 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. Link to comment https://forums.phpfreaks.com/topic/242464-selecting-part-of-a-string/#findComment-1245324 Share on other sites More sharing options...
jeff5656 Posted July 20, 2011 Author Share Posted July 20, 2011 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 Link to comment https://forums.phpfreaks.com/topic/242464-selecting-part-of-a-string/#findComment-1245354 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.