开发者

getting only certain data from table

I have a field that is in this format

5551112391^HUMAN^HUMAN-800-800^6-main^^

How would I only grab th开发者_Python百科e numbers 5551112391 before the character ^?

Would you do this with regex?


You can make use of explode:

$var = '5551112391^HUMAN^HUMAN-800-800^6-main^^';
$arr = explode('^',$var);
$num = $arr[0]; 

Using regex:

$var = '5551112391^HUMAN^HUMAN-800-800^6-main^^';
if(preg_match('/^(\d+)/',trim($var),$m)){
   $num = $m[1]; 
}


Regex overkill, nice...

What about simple cast to int? Will work perfectly OK if the number is in the beginning of data. And definitely faster than regexps...

$var = '5551112391^HUMAN^HUMAN-800-800^6-main^^';
$num = (int)$var;

http://www.php.net/manual/en/language.types.type-juggling.php


You're doing it in completely wrong way.
You treat mysql database as a flat text file. But it is not.

  1. All these fields must be separated and stored in separate columns.
  2. To get only certain data from the table, you should not select all rows and then compare one by one but make database do it for you:

    SELECT * FROM table WHERE number=5551112391

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜