jeff5656 Posted July 20, 2011 Share Posted July 20, 2011 If I have this string: tester123@somplace.com How do I do it so that I can echo out: tester123 Thanks! Quote 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: tester123@somplace.com 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. Quote 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 Quote 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? Quote 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. Quote 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 Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.