farkewie Posted November 15, 2007 Share Posted November 15, 2007 Hey ive seen it somewhere but i just cant find it.. i need to read a string of nine numbers and depending on the value of the first number set a variable saying if it is 3g or not ie. <?php if ($phone_row['sim'] starts with 5 or higher) { $network = "3G / Next G";} else if ($phone_row['sim'] starts with 4 or lower) { $network = "GSM"; } ?> Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 15, 2007 Share Posted November 15, 2007 this should do it if (preg_match("/^[5-9]/",$phone_row['sim'])) { $network = "3G / Next G";} else if (preg_match("/^[1-4]/",$phone_row['sim'])) { $network = "GSM"; } Quote Link to comment Share on other sites More sharing options...
kratsg Posted November 15, 2007 Share Posted November 15, 2007 Wow, raji, we don't need the preg match anymore... Simply do the following: $first_number = $phone_row['sim'][0];//if it is a string, returns the first character if($first_number >= 5){$network = "3G / Next G";} if($first_number < 5){$network = "GSM";} Here's some reference links if needed: http://us.php.net/types.string Quote Link to comment Share on other sites More sharing options...
farkewie Posted November 15, 2007 Author Share Posted November 15, 2007 Ok thanks guys they both work!! But for he purpose of my learning... which would be better practice to use? Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 15, 2007 Share Posted November 15, 2007 Well both are good one uses regular expression and one uses the string functions depending upon your need I mostly use regular expression anywhere I can Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted November 15, 2007 Share Posted November 15, 2007 Without bench marking it... regular expression is not as efficienct as the method kratsg suggested for the simple fact it has to run the regex engine on a string. If you KNOW the firts value of the 'string' is the piece of data you need then its much more efficient to isolate that piece of data without the use of a regular expression. Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 15, 2007 Share Posted November 15, 2007 Did not know that... learnt something today Quote Link to comment 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.