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"; } ?> Link to comment https://forums.phpfreaks.com/topic/77471-solved-get-the-fist-number-froma-string/ 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"; } Link to comment https://forums.phpfreaks.com/topic/77471-solved-get-the-fist-number-froma-string/#findComment-392174 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 Link to comment https://forums.phpfreaks.com/topic/77471-solved-get-the-fist-number-froma-string/#findComment-392182 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? Link to comment https://forums.phpfreaks.com/topic/77471-solved-get-the-fist-number-froma-string/#findComment-392186 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 Link to comment https://forums.phpfreaks.com/topic/77471-solved-get-the-fist-number-froma-string/#findComment-392193 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. Link to comment https://forums.phpfreaks.com/topic/77471-solved-get-the-fist-number-froma-string/#findComment-392202 Share on other sites More sharing options...
rajivgonsalves Posted November 15, 2007 Share Posted November 15, 2007 Did not know that... learnt something today Link to comment https://forums.phpfreaks.com/topic/77471-solved-get-the-fist-number-froma-string/#findComment-392205 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.