String converted into numbers? -Php Tutorial

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 numbers containing quotation marks is converted to numbers: 02865066306, SIP & nbsp; Phone, IP & nbsp; Trunk, trunk20, ON2OFF, 1333230069,1333230072, 13 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.

------ Best 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.
------ Other solutions --------------------
Isn't it necessary to be so troublesome?
$arr = '"1000","902865066306","02865066306","SIP Phone","IP Trunk","","trunk20","ON2OFF","1333230069","1333230072","1333230134","65","62","Connected"';
$t = json_decode("[$arr]");
print_r($t);
Array
(
[0] = & gt; 1000
[1] => 902865066306.
[2] => 02865066306.
[3] => SIP Phone
[4] => IP Trunk
[5] =>
[6] => trunk20
[7] => ON2OFF
[8] = & gt; 1333230069
[9] = & gt; 1333230072
[10] => 1333230134.
[11] => 65
[12] => 62
[13] => Connected
)

Yes.
$ T = array_map (function ($ s) {return trim ($ s, '"') ;}, explode (',', $ arr )); //> = php 5.3
Yes.
$ T = array_map (create_function ('$ S', 'Return trim ($ s, \' "\ ');'), explode (',', $ arr )); //> = php 4

Directly process the data into available arrays.
------ Other solutions --------------------
Your questions
Good
$ Num = "1333230069 ";
Echo $ num + 0; /// 1333230069

But what is $ arr [8?

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.