uswebpro Posted October 28, 2006 Share Posted October 28, 2006 I have a string the has either 2,3 or 4 numbers then 3 characters.ie 88fry, 123tyd, 1256twqHow can I get the values of the seperate parts?like:$1 = 1256;$2 = twq;thanks! Quote Link to comment https://forums.phpfreaks.com/topic/25398-quick-regex-help/ Share on other sites More sharing options...
shoz Posted October 28, 2006 Share Posted October 28, 2006 If by "characters" you mean letters from a-z.[code]<?php$string = '111abc';if (preg_match('/(^\d{2,4})([a-z]{3})$/i', $string, $matches)){ $numbers = $matches[1]; $letters = $matches[2]; print $numbers."<br />\n"; print $letters."<br />\n";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/25398-quick-regex-help/#findComment-115918 Share on other sites More sharing options...
uswebpro Posted October 28, 2006 Author Share Posted October 28, 2006 awesome! thanks Quote Link to comment https://forums.phpfreaks.com/topic/25398-quick-regex-help/#findComment-116081 Share on other sites More sharing options...
Nicklas Posted October 28, 2006 Share Posted October 28, 2006 There´s no need for regular expressions to do what you want, take a look at the [url=http://www.php.net/sscanf]sscanf()[/url] function.ex:[code]<?php$string = '1256twq';list($numbers, $letters) = sscanf($string, "%d%s");echo "Numbers: $numbers<br />Letters: $letters";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/25398-quick-regex-help/#findComment-116082 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.