geroido Posted September 3, 2008 Share Posted September 3, 2008 Hi I have entries in my database that look like the following: lynnie20080825122012 geroido20080825105322 lynnie20080825112208 geroido20080825122526 They are all unique entries. What I want to do is to extract only the first alphabetical part of the string and nothing else. So I would end up with: lynnie geroido lynnie geroido Any ideas how I might do this? Link to comment https://forums.phpfreaks.com/topic/122536-how-to-extract-only-first-alphabetical-characters-from-a-string/ Share on other sites More sharing options...
Fadion Posted September 3, 2008 Share Posted September 3, 2008 I'm a total noob in regular expressions, but this should do it: <?php $str = 'lynnie20080825122012'; $str = preg_replace('/[0-9]/', '', $str); echo $str; ?> Link to comment https://forums.phpfreaks.com/topic/122536-how-to-extract-only-first-alphabetical-characters-from-a-string/#findComment-632670 Share on other sites More sharing options...
geroido Posted September 3, 2008 Author Share Posted September 3, 2008 Hi Guiltygear Can you explain what this is doing exactly Thanks <?php $str = 'lynnie20080825122012'; $str = preg_replace('/[0-9]/', '', $str); echo $str; ?> Link to comment https://forums.phpfreaks.com/topic/122536-how-to-extract-only-first-alphabetical-characters-from-a-string/#findComment-632680 Share on other sites More sharing options...
Fadion Posted September 3, 2008 Share Posted September 3, 2008 Sure . It is just replacing every number in the string with nothing. The regular expression is doing that, searching and replacing every number ([0-9]), leaving just the text. Link to comment https://forums.phpfreaks.com/topic/122536-how-to-extract-only-first-alphabetical-characters-from-a-string/#findComment-632682 Share on other sites More sharing options...
Fadion Posted September 3, 2008 Share Posted September 3, 2008 Ok as you get the data from a database, i'm going for a full example: <?php $results = mysql_query("SELECT username FROM users"); while($values = mysql_fetch_array($results)){ $user = preg_replace('/[0-9]/', '', $values['user']); echo $user; } ?> Link to comment https://forums.phpfreaks.com/topic/122536-how-to-extract-only-first-alphabetical-characters-from-a-string/#findComment-632686 Share on other sites More sharing options...
geroido Posted September 3, 2008 Author Share Posted September 3, 2008 Hi Gulitygear I just tested that and it works brilliantly. Thanks a lot geroido Link to comment https://forums.phpfreaks.com/topic/122536-how-to-extract-only-first-alphabetical-characters-from-a-string/#findComment-632688 Share on other sites More sharing options...
Fadion Posted September 3, 2008 Share Posted September 3, 2008 Glad to hear that. Please mark the topic as solved if you don't have anymore questions. Link to comment https://forums.phpfreaks.com/topic/122536-how-to-extract-only-first-alphabetical-characters-from-a-string/#findComment-632696 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.