jeff5656 Posted December 31, 2009 Share Posted December 31, 2009 I have a variable $row['name']. Sometimes the string is smith, John. How do I use regex to only include up to the comma, i.e. $newlastname = smith? Quote Link to comment https://forums.phpfreaks.com/topic/186818-include-everything-up-to-the-comma/ Share on other sites More sharing options...
salathe Posted December 31, 2009 Share Posted December 31, 2009 One way (of many) would be: $newlastname = $row['name']; if (preg_match('/^[^,]+(?=,)/', $row['name'], $match)) { $newlastname = $match[0]; } If you're using PHP 5.3 then you could instead use strstr and even if not, basic string functions might be easier, cleaner, etc. in this case. Quote Link to comment https://forums.phpfreaks.com/topic/186818-include-everything-up-to-the-comma/#findComment-986551 Share on other sites More sharing options...
.josh Posted December 31, 2009 Share Posted December 31, 2009 or just explode it... Quote Link to comment https://forums.phpfreaks.com/topic/186818-include-everything-up-to-the-comma/#findComment-986584 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.