timmah1 Posted May 28, 2009 Share Posted May 28, 2009 If I have a variable in the database like this 83&sports=nfl How can I show just the number and not the rest of the stuff. This will always show a number and the &sports= whatever sport, the number could be in the hundreds Thanks in advance Link to comment https://forums.phpfreaks.com/topic/160039-solved-extract-only-number/ Share on other sites More sharing options...
Ken2k7 Posted May 28, 2009 Share Posted May 28, 2009 Putting aside the bad DB design. <?php $str = '83&sports=nfl'; $parts = explode('&', $str); echo $parts[0]; Link to comment https://forums.phpfreaks.com/topic/160039-solved-extract-only-number/#findComment-844254 Share on other sites More sharing options...
timmah1 Posted May 28, 2009 Author Share Posted May 28, 2009 Wasn't my design, just trying to get it working better. Would this work as well? $string = ereg_replace("[^0-900]", "", $favorite); Link to comment https://forums.phpfreaks.com/topic/160039-solved-extract-only-number/#findComment-844258 Share on other sites More sharing options...
Ken2k7 Posted May 28, 2009 Share Posted May 28, 2009 I don't find the need of regular expression for this because they're more intensive than necessary, but if you really want it... <?php $str = '83&sports=nfl'; preg_match('#^(\d+)#', $str, $match); echo $match[0]; preg > ereg. Link to comment https://forums.phpfreaks.com/topic/160039-solved-extract-only-number/#findComment-844267 Share on other sites More sharing options...
premiso Posted May 28, 2009 Share Posted May 28, 2009 preg > ereg. To elaborate. As of PHP6 ereg is depreciated. Also preg tends to be more universal since it users perl regular expressions. I would avoid using ereg due to the fact it is depreciated. Link to comment https://forums.phpfreaks.com/topic/160039-solved-extract-only-number/#findComment-844286 Share on other sites More sharing options...
timmah1 Posted May 28, 2009 Author Share Posted May 28, 2009 Thank you for the help, I really appreciate it Link to comment https://forums.phpfreaks.com/topic/160039-solved-extract-only-number/#findComment-844294 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.