Convert a string to a number? How to solve

Source: Internet
Author: User
Convert a string to a number ??? Today, I encountered a strange phenomenon. it is very likely that I have not enough reserves. a problem occurs when the string of a number containing quotation marks is converted to a number. "1000", "902865066306 ", "02865066306", "SIP & nbsp; Phone", "IP & nbsp; Trunk", "", "trunk20", "ON2OFF", string converted into numbers ???
Today, I encountered a strange phenomenon. it is very likely that I have not enough reserves. a problem occurs when the string containing the quotation marks is converted to a number.


"1000","902865066306","02865066306","SIP Phone","IP Trunk","","trunk20","ON2OFF","1333230069","1333230072","1333230134","65","62","Connected"

The above is a piece of data, and the next operation

$arr = '"1000","902865066306","02865066306","SIP Phone","IP Trunk","","trunk20","ON2OFF","1333230069","1333230072","1333230134","65","62","Connected"';
$arr = explode(',',$arr);
print_r($arr);

Print

Array
(
    [0] => "1000"
    [1] => "902865066306"
    [2] => "02865066306"
    [3] => "SIP Phone"
    [4] => "IP Trunk"
    [5] => ""
    [6] => "trunk20"
    [7] => "ON2OFF"
    [8] => "1333230069"
    [9] => "1333230072"
    [10] => "1333230134"
    [11] => "65"
    [12] => "62"
    [13] => "Connected"
)

I want to compare the ninth character in the array with other data. when I convert the string enclosed by quotation marks into numbers, it is okay to add 0 to the back. However, today is different.

Echo $ arr [8] + 0; // The result is 0.

I was wondering, why is it 0? $ arr [8] clearly has a value, so I tested it again.

$num = "1333230069";
echo $num+0;//1333230069

After a long time, I didn't understand what was going on. then I used two methods to solve the problem. one was to use regular expressions to propose numbers, the other is to replace all double quotation marks in the string with null at the beginning.

But the doubt has not been solved. until now, I am wondering why the above practice produces 0 results rather than the expected number.

------ Solution --------------------
Var_dump ($ arr [8]); // string (12) "" 1333230069 ""
Var_dump ($ num); // string (10) "1333230069"

In this way, we can clearly understand that the double quotation marks at the beginning and end of $ arr [8] are removed before the operation.
------ Solution --------------------
$ A = str_getcsv ($ arr );
Var_dump ($ a [8]);


------ Solution --------------------
Based on the information you provided, I also tested it myself:
 $arr= Array
(0 => "1000",
    1 => "902865066306",
    2 => "02865066306",
    3 => "SIP Phone",
    4 => "IP Trunk",
    5 => "",
    6 => "trunk20",
    7 => "ON2OFF",
    8 => "1333230069",
    9 => "1333230072",
    10 => "1333230134",
    11 => "65",
    12 => "62",
    13 => "Connected"
);
echo gettype($arr[8])."
";
echo $arr[8]."
";
$str1=$arr[8]+0;
echo gettype($str1)."
";
echo ($str1)."
";

$num = "1333230069";
$num1=($num+0);
echo gettype($num)."
";
echo gettype($num1)."
";
echo $num1;

?>


The output result is:
String
1333230069
Integer
1333230069
String
Integer
1333230069

No problem was found.
------ Solution --------------------
Isn't it necessary to be so troublesome?
$arr = '"1000","902865066306","02865066306","SIP Phone","IP Trunk","","trunk20","ON2OFF","1333230069","1333230072","1333230134","65","62","Connected"';

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.