Jump to content

[SOLVED] Get the fist number froma string?


farkewie

Recommended Posts

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

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.