Php number from string to int. Manipulating PHP Data Types


PHP does not require (or support) an explicit type definition when declaring a variable; the type of a variable is determined according to the context in which it is used. That is, if you assign a string value to $var , $var will become a string. If you then assign $var an integer value, it becomes an integer.

An example of automatic type conversion is the addition operator " + ". If any of the operands is a floating point number, then all the operands are interpreted as floating point numbers, the result will also be a floating point number. Otherwise, the operands will be interpreted as integers and the result will also be an integer. Note that this DOES NOT change the types of the operands themselves, only how they are evaluated Some practical examples:

$foo = "0" ; // $foo is a string (ASCII 48)
$foo += 2 ; // $foo is now an integer (2)
$foo = $foo + 1.3 ; // $foo is now a float (3.3)
$foo = 5 + "10 Little Piggies" ; // $foo is an integer (15)
$foo = 5 + "10 Small Pigs" ; // $foo is an integer (15)
?>

If you want to test any of the examples given in this section, you can use the function var_dump() .

Attention: -1 counts TRUE, like any non-zero (negative or positive) number!

Examples of transformations:

var_dump ((bool) "" ); //bool(false)
var_dump ((bool) 1 ); //bool(true)
var_dump ((bool) - 2 ); //bool(true)
var_dump ((bool) "foo" ); //bool(true)
var_dump ((bool) 2.3e5 ); //bool(true)
var_dump ((bool) array(12 )); //bool(true)
var_dump((bool) array()); //bool(false)
var_dump ((bool) "false" ); //bool(true)
?>

Conversion from Boolean

FALSE is converted to 0 (zero), and TRUE- in 1 (unit).

Conversion from Float

When converting from a floating point number to an integer, the number will be rounded towards zero.

If the float exceeds the limits of the integer (typically +/- 2.15e+9 = 2^31), the result will be undefined because the integer does not have enough precision to return the correct result. In this case, no warning or even remark will be displayed.

Attention! Never convert an unknown fraction to a whole, as this can sometimes give unexpected results, for example:

echo (int) ((0.1 + 0.7 ) * 10 ); // prints 7!
?>

Conversion from String

Conversions from other types

For other types, the conversion behavior to an integer is undefined. Currently the behavior is the same as if the value had first been . However Not Rely on this behavior as it may change without warning.

Boolean ( boolean) meaning TRUE is converted to the string "1" and the value FALSE is represented as "" (empty string). In this way, you can convert values ​​in both directions - from Boolean to string and vice versa.

Integer ( integer) or floating point number ( float) is converted to a string represented by a number consisting of its digits (including the exponent for floating point numbers).

Arrays are always converted to the string "Array" , so you can't display the contents of the array ( array), using echo() or print() to see what it contains. To view a single element you need to do something like echo $arr["foo"] . See below for tips on how to display/view all content.

Objects are always converted to the string "Object" . If you want to print the value of an object's member variable ( object) For debugging purposes, read the following paragraphs. If you want to get the class name of the required object, use get_class() .

Resources are always converted to strings with the structure "Resource id #1" , where 1 is unique number resource ( resource) assigned to it by PHP at runtime. If you want to get the resource type, use .

NULL is always converted to the empty string.

Printing arrays, objects, or resources does not provide you with any useful information about the meanings themselves. A better way to output values ​​for debugging is to use functions print_r() And var_dump() .

You can also convert PHP values into strings for permanent storage. This method is called serialization and can be done using the function serialize(). In addition, if in your PHP installation There is WDDX support, you can serialize PHP values ​​into XML structures.

Conversion to Array type

For any of the types: integer, float, string, boolean And resource if you convert the value to an array, you will end up with an array with one element (at index 0) being the scalar value you started with.

If you convert an object to an array ( object), you will get the properties (member variables) of this object as array elements. The keys will be the names of the member variables.

If you convert the value to an array NULL, you will get an empty array.

Conversion to Object type

If an object is converted to an object, it is not modified. If a value of any other type is converted to an object, a new instance of the built-in class stdClass is created. If the value was empty, the new instance will also be empty. For any other value, it will be contained in a scalar member variable:

$obj = (object) "ciao" ;
echo $obj -> scalar ; // will print "ciao"
?>

Information has always been represented by symbols. Only when programming appeared did it partially become letters, numbers and signs (in the formal sense).

Until a certain time, the most popular were oral speech, hand-made symbols, gestures, as well as works of art, in particular. It is important that all these options for the natural representation of information, from small to large, could always be described in words, that is, made accessible to software processing in one sense or another.

Strings and numbers

Strings are, first of all, collections various characters. IN different means programming fraction separated from the whole in different ways. Some preferred a period, others a comma (in PHP, converting a string to a number implies "."). Triads were separated by a single apostrophe, comma, or space. Real numbers were written with the letter "E".

All these are formalities. Mathematics and the principles of operation of any processor have always recognized numbers without frills, and corresponding additional processors have been developed to work with real values.

But the world of concrete type variables (for early era programs) and implementation modern idea consensus of all data types means: in PHP, converting a string to a number is the same as doing nothing. In the best case, PHP will “cut off”, or rather, make 0 or a number, if there is at least one digit in the source string. All digits up to the first non-digit character will add up to this number.

General conversion rule

In PHP, the conversion of a string to a number, unless the programmer has specified otherwise, occurs automatically at the time of application. If the variable contains only numbers and a period, then it can be obtained real number. If a comma is encountered in a sequence of numbers, then everything that comes before it will be interpreted as a number, naturally, an integer.

Any non-numeric character in the sequence of numbers stops the process, and the result of PHP's string-to-number conversion will be only the value that formed before the first non-numeric character.

Mathematics and symbol processing

What is more difficult, the first or the second, even from the standpoint of formulas that do not fit on the page, impressive matrices, graphs that sometimes even the authors cannot untangle, derivatives of integrals, differentials of integrals and polynomials in the nth generation, it is very simple to say: society , of course, honors the merits of mathematics and mathematicians, but the processing of symbols is more important, more complex and covers mathematics like a duvet, providing an inquisitive mind that cannot go beyond the magic of numbers, a favorable environment for creativity.

Usually on PHP string is converted to a number automatically, but it's never a bad idea to check this. It's best to always be sure that PHP correctly understands that a given variable is a number. If there is even a shadow of doubt, you should explicitly specify the type of the variable (int) or (float) before the value and check how PHP understands the type variable functions is_integer() or is_float().

Modern programming trends

The classic interpretation of the PHP “string to number” problem lies within the concepts of modern programming. The reverse process has much more varied options. Functions number_format(), printf(), sprintf() can work wonders, not only at the level of one number, but also at a group of numbers.

Conversion to PHP" line to number" indicates, rather, that we can do this too, but m mathematics and numerical calculations are not our strong point.

Writing decent and reliable functionality that implements this or that number processing in PHP is not a problem, and the result will not be any worse than the result from more mathematical languages. PHP is, first of all, a server language, a language for processing information, including mathematical information.

Among various programming tasks, various manipulations with date and time values ​​are quite common. Rare automated system, the database can do without storing information about the time of a particular process. In addition to the simplest addition of a date to a database record or displaying this date, there are many tasks of displaying these dates in in various forms, checking whether the current time matches set timer, calculating the period between two dates and much more.

To make working with dates easier, each programming language has its own special data types for storing date and time values. Most often this numeric value, either integer or floating point.

IN PHP work with a date, it most often encounters UNIX TIMESTAMP. Here the time is stored as an integer. The calculation of time begins on January 1, 1970. Therefore, for example, the date and time 12/11/2014 19:40:00 would be represented by the number 1418316000. This number shows how many seconds have passed since the zero date of January 1, 1970, called the Unix Epoch.

An example of a PHP page that provides data conversion capabilities is presented on the website in the programs section with the program "Convert date and time format". Here you can generate the desired date in the UNIX TIMESTAMP format, as well as bring this format into a standard, human-readable form.

Getting current time and date in PHP

To get the current server time, use the function

int time(void)

which will return the value in unix timestamp format.

echo time(); // output the current time in unix timestamp format

At first glance, the format is not very convenient for humans, but, as you know, the simpler the data presentation, the faster the computer processes these values. Also, storing a number in a database is much more cost effective than any special format. Also, PHP works the same over time on both Unix and Windows platform, which makes it possible to use the code on any of these platforms.

Convert date and time format in PHP

The simplest mechanism for converting a numeric date value into more understandable values ​​is provided by the function:

array getdate()

She returns associative array, containing date information. If the timestamp parameter is not specified, the current time will be returned. This array contains the following values:

The resulting array allows you to display the values ​​in the desired form:

$date = 1418372345;
$date_mas = getdate($date);
echo $date_mas["mday" ] . " . " . $date_mas["mon" ] . " . " . $date_mas["year" ]; // 12.12.2014


You can also use the function to convert the date and time format:

string date(string $template [, int $unix_timestamp])

It is designed to obtain the current unix timestamp date in the required format. The $template string parameter specifies the output format. The $unix_timestamp parameter can be used to specify which time value to work with. It is optional, so if it is not specified it will be used The current date and time.

The format is specified by the following values:

a"before" and "after" noon: "am" or "pm"
A"before" and "after" noon in capital letters: "AM" or "PM"
dday of the month in 2 digits (if less than 10, zero comes first) (01 to 31)
Dday of the week in 3 letters. For example, "Mon" (Monday)
jday of the month, 1-2 digits without leading zeros (from 1 to 31)
Fname of the month. For example, "January"
hhour, 12-hour format (01 to 12)
Hhour, 24-hour format (00 to 23)
ghour, 12-hour format without zeros (1 to 12)
Ghour, 24-hour format without zeros (0 to 23)
iminutes (00 to 59)
I (capital i)1 if daylight saving time is in effect, otherwise 0
L1 if the year is a leap year, or 0 if it is not a leap year
Btime in Internet time format ( alternative system time of day countdown) (from 000 to 999)
Tcomputer time zone. For example, MDT
l (lowercase L)day of the week. For example, "Monday"
mmonth, two digits with zeros (01 to 12)
nmonth, one or two digits without zeros (from 1 to 12)
Mabbreviated name of the month. For example, "Jan"
tnumber of days in the specified month (from 28 to 31)
sseconds (0 to 59)
SEnglish two-letter ordinal number suffix ("st", "nd", "rd" or "th")
Uinteger number of seconds since the start of the UNIX epoch
yyear, digital, 2 digits (14)
Yyear, digital, 4 digits (2014)
zordinal number of the day in the year (from 0 to 365)
Ztime zone offset in seconds (-43200 to 43200)
Nserial number days of the week from 1 (Monday) to 7 (Sunday) in accordance with the ISO-8601 standard, (added to PHP versions 5.1.0)
wserial number of the day of the week from 0 (Sunday) to 6 (Saturday)
Wserial number of the week of the year in accordance with the ISO-8601 standard; weeks start on Monday (added in PHP 4.1.0)
oyear number according to ISO-8601 standard. Has the same meaning as Y, except when the ISO week number (W) is from the previous or following year; then the year of that week will be used. (added in PHP 5.1.0)
etime zone scale code. For example: UTC, GMT, Atlantic/Azores (added in PHP 5.1.0)
Odifference with Greenwich time, in hours. For example: +0200
Pdifference from Greenwich Mean Time with a colon between hours and minutes. For example: +02:00 (added in PHP 5.1.3)
cdate in ISO 8601 standard format. For example, 2014-12-12T15:19:21+00:00 (added in PHP 5)
rdate in » RFC 2822 format. For example: Thu, 21 Dec 2000 16:01:07 +0200
Unumber of seconds that have passed since the beginning of the Unix Epoch (The Unix Epoch, January 1, 1970 00:00:00 GMT)

As you can see from the list, you can get a lot of useful date data using this function. For example:

$date = 1418372345; // initial date and time 12/12/2014 11:19:05

echo date("d.m.Y" , $date); // 12.12.2014 (date)
echo date("H:i:s" , $date); // 11:19:05 (time)
echo date("H:i" , $date); // 11:19 (time)
echo date("t" , $date); // 31 (number of days in a month)
echo date("z" , $date); // 345 (ordinal number of the day in the year)
echo date("l dS \of F Y h:i:s A" , $date); // Friday 12th of December 2014 11:19:05 AM

Other characters included in the pattern will be printed as is on the line. If you need to enter a character that is used in a function as a format code, the “\” character is inserted in front of them. For the value "\n" (the transition character new line), you should specify "\\n". Thus, you can output an entire message containing date and time information:

echo date( "Today z-th day Y year", $date); // Today is the 345th day of 2014

Convert date and time to timestamp format

To convert a date back from a standard format to a numeric timestamp value, use the function:

int mktime(]]]]]])

The mktime() function returns the Unix time value corresponding to the date and time specified by its arguments. For example:

$my_date = mktime(10, 30, 0, 12, 12, 2014);

You should be careful about the order of the function arguments: hours, minutes, seconds, month, day, year.

In addition to simply generating a date value into a timestamp, the mktime() function can be used to perform arithmetically with dates. To do this, you can simply enter the necessary arguments. For example, if you specify the 14th month, then in the final value the month will be the 2nd, and the year value will increase by one:

$my_day = 12;
$my_month = 12;
$my_year = 2014;

$new_date = mktime(0, 0, 0, $my_month + 5, $my_day, $my_year);

echo date("d.m.Y" , $new_date); // 05/12/2015 (date)

You can do the same with other parameters.

Checking date validity in PHP

When working with dates, especially when generating a date using the mktime() function proposed above, it is necessary to take into account the correctness of the entered date. To do this, PHP uses the function:

bool checkdate(int month, int day, int year)

Returns true if the date specified by the arguments is correct; otherwise it returns false. The date is considered correct if:

Year in the range from 1 to 32767;

Month ranges from 1 to 12;

The day for a given month, taking into account leap years, is indicated correctly.

Date checking example:

$my_day = 32;
$my_month = 12;
$my_year = 2014;

if (!checkdate($my_month, $my_day, $my_year))
echo "Error: the date is incorrect";

I have an application that touches clients from all over the world, and naturally I want everything that goes into my databases to be UTF-8 encoded.

The main problem for me is that I don't know what the source encoding of any string will be - it could be from a text field (using

only useful if the user actually submitted the form), or it could be from an uploaded text file, so I really can't control the input.

I need a function or class that ensures that the stuff coming into my database is, as far as possible, UTF-8 encoded. I tried iconv(mb_detect_encoding($text), "UTF-8", $text); but this has problems (if the input is "fiancée", it returns "fianc"). I tried many things =/

For file uploads I like the idea of ​​asking the end user to specify the encoding they are using and showing them preview of what the output will look like, but that doesn't help against nasty hackers (it might actually make their life a little easier).

I've read other SO questions on this subject, but they seem to have subtle differences such as "I need to parse RSS feeds" or "I remove data from websites" (or, indeed, "You can't") .

But there must be something that at least, has a nice try!

9 answers

What you are asking is extremely difficult. If possible, it is best to set the encoding to the user. Preventing an attack doesn't have to be much easier or more difficult.

However you can try:

Iconv(mb_detect_encoding($text, mb_detect_order(), true), "UTF-8", $text);

Setting a strict value can help you get a better result.

In our homeland of Russia we have 4 popular encodings, so your question is very popular here.

With char character codes alone you can't detect the encoding because the code pages overlap. Some code pages on different languages even have complete intersection. So, we need a different approach.

The only way to work with unknown encodings is to work with probabilities. So we don't want to answer the question "what is the encoding of this text?", we are trying to understand " what is most likely the encoding of this text?".

One guy from a popular Russian technology blog came up with this approach:

Create a probability range of char codes in each encoding you want to support. You can build it using some great texts in your language (like some fiction, use Shakespeare for English and Tolstoy for Russian, lol). You will get something like this:

Encoding_1: 190 => 0.095249209893009, 222 => 0.095249209893009, ... encoding_2: 239 => 0.095249209893009, 207 => 0.095249209893009, ... encoding_N: charcode = > probabilty

Further. You take text in an unknown encoding and for each encoding in your "probability dictionary" you look up the frequency of each character in the text with the unknown encoding. Total probability of symbols. The coding with the highest rating is probably the winner. top scores for large texts.

If you are interested, I can happily help you solve this problem. We can significantly improve accuracy by creating a two-character list of probabilities.

Btw. mb_detect_encoding certanly doesn't work. Yes, in general. Please have a look source mb_detect_encoding in "ext/mbstring/libmbfl/mbfl/mbfl_ident.c".

You've probably tried this, but why not just use the mb_convert_encoding function? It will try to automatically determine the char set of the provided text, or you can pass it a list.

Additionally, I tried running:

$text = "fiancée"; echo mb_convert_encoding($text, "UTF-8"); echo "

"; echo iconv(mb_detect_encoding($text), "UTF-8", $text);

and the results are the same for both. How do you see your text being truncated to "fianc"? is it in the database or in the browser?

It is impossible to identify a string encoding that is completely accurate. There are ways to try to guess the encoding. One of these methods, and probably/currently the best in PHP, is mb_detect_encoding(). This scans your string and looks for occurrences of things unique to certain encodings. Depending on your string there may not be such distinguishable cases.

Take ISO-8859-1 and ISO-8859-15 encoding (http://en.wikipedia.org/wiki/ISO/IEC_8859-15#Changes_from_ISO-8859-1)

There are only a few different characters, and to make them worse, they are represented by the same bytes. It is impossible to discover, given a string without knowing its encoding, whether byte 0xA4 should represent ¤ or € in your string, so there is no way to know its exact encoding.

(Note: you could add a human element or an even more advanced scanning method (like what Oroboros102 suggests) to try to figure out, based on the surrounding context, if the character should be ¤ or €, although that seems like a bridge too far)

There are more distinguishable differences between, for example, UTF-8 and ISO-8859-1, so it's still worth trying to figure it out when you're unsure, although you can and shouldn't rely on it being correct.

There are other ways to ensure correct encoding. As for forms, try to force UTF-8 as much as possible (check Snowman to make sure you will represent UTF-8 in every browser: http://intertwingly.net/blog/2010/07/29/Rails-and-Snowmen) This is done so at least you can be sure that every text submitted through your forms is utf_8. For downloaded files, try running the unix command "file -i" on it, for example. exec() (if possible on your server) to aid discovery (using the document specification). As for scraping data, you can read the HTTP headers, which usually determine the encoding. When analyzing XML files see If the XML metadata contains a character set definition.

Instead of trying to automatically guess the encoding, you should first try to provide the specific encoding yourself if possible, or try to get the definition from the source you get it from (if applicable) before resorting to detection.

The main problem for me is that I don't know what the source encoding of any string will be - it could be from a text field (only useful if the user has actually submitted the form), or it could be from a loaded text file, so I really can't control the input.

I don't think this is a problem. The application knows the input source. If it's from a form, use UTF-8 encoding in your case. It works. Just make sure the data provided is encoded correctly (check). Keep in mind that not all databases fully support UTF-8.

If it's a file, you won't save it in UTF-8 encoding in the database, but in binary form. When you output the file again, use binary output too, then it's completely transparent.

Your idea is good that the user can tell the encoding if he/she can tell after downloading the file anyway since it is binary.

So I have to admit that I don't see specific problem which you raise with your question. But maybe you can add some details about your problem.

Grim, there are some really good answers and attempts to answer your question here. I would like to thank everyone for their responses. They are gorgeous. I'm not a coding master, but I understand your desire to have a pure UTF-8 stack in your database. I'm using MySQL utf8mb4 for tables, fields and connections.

My situation boiled down to "I just want my sanitizers, validators, business logic and prepared statements to handle UTF-8 when the data comes from HTML forms or links to register by e-mail". So, I think I started with this idea:

  • Trying to detect encoding: $encodings = ["UTF-8", "ISO-8859-1", "ASCII"];
  • If the encoding cannot be detected, throw new RuntimeException
  • If the input is UTF-8, continue.
  • Otherwise if it is ISO-8859-1 or ASCII

    A. Attempting to convert to UTF-8 (pending, not completed)

    b. Determine the encoding of the converted value

    With. If the reported encoded and converted value is UTF-8, proceed.

    e. Else, throw new RuntimeException

From my abstract class Sanitizer

Private function isUTF8($encoding, $value) ( ​​return (($encoding === "UTF-8") && (utf8_encode(utf8_decode($value)) === $value)); ) private function utf8tify(&$ value) ( ​​$encodings = ["UTF-8", "ISO-8859-1", "ASCII"]; mb_internal_encoding("UTF-8"); mb_substitute_character(0xfffd); //REPLACEMENT CHARACTER mb_detect_order($encodings); $stringEncoding = mb_detect_encoding($value, $encodings, true); if (!$stringEncoding) ( $value = null; throw new \RuntimeException("Unable to identify character encoding in sanitizer."); ) if ($this-> isUTF8($stringEncoding, $value)) ( return; ) else ( $value = mb_convert_encoding($value, "UTF-8", $stringEncoding); $stringEncoding = mb_detect_encoding($value, $encodings, true); if ($ this->isUTF8($stringEncoding, $value)) ( return; ) else ( $value = null; throw new \RuntimeException("Unable to convert character encoding from ISO-8859-1, or ASCII, to UTF-8 in sanitizer ."); ) ) return; )

An argument could be made that I should decouple the encoding concerns from my abstract Sanitizer class and simply insert an Encoder object into a concrete child Sanitizer instance. However, the main problem with my approach is that I, without any knowledge, reject coding types that I don't need (and I rely on PHP functions mb_ *). Without further study, I can't know if some people are hurt or not (or if I'm losing important information). So I need to find out more. I found this article.

Integer numbers do not have priority over real numbers, but they are destined for a largely “non-digital” purpose. An exact number with a specific number of digits and/or in a specific range of values ​​makes sense to form CSS rules, can be used as an array key or a unique identifier for a row entry in a database table. The integer can be part of a unique code, a password, or checksum when transferring data. An integer is far from being a matter of mathematics and numerical methods; it is often an element of a data or its code.

The meaning of the intval() function

The intval() PHP function is used to convert a “variable to an integer” and has two parameters and a double meaning.

The second parameter is the base of the required number system. Default is decimal system. If you do not specify the second parameter, then a number that begins with the character “0” is considered octal, and those starting with the character “0x” are considered hexadecimal.

The second parameter has a value if the first parameter is a character string.

The last two examples (very large values) show that the result is inappropriate. Essentially, what matters here is the bit capacity of the machine ( operating system). In practice, conversions of such numbers do not make much sense.

The logic behind the PHP intval function is to convert a string or number to an integer for practical application. For example, create a CSS rule that only uses integers. Convert a real number to an integer by simply removing only the integer part.

Using the intval() function

The intval() function in PHP is not the only one for the purpose of getting an integer. You can use round(), ceil() and floor(). These functions operate with generally accepted rounding logic.

The intval() PHP function is devoid of any logic and acts on the principle of getting an integer. If required, then for the right reason the desired system Reckoning.

The given examples of intval() PHP show that its main area of ​​​​application is the logic of the algorithm, and not the logic of calculations.

This function - perfect tool generating data for correct display HTML pages(use in CSS rules parameters and attributes HTML tags) and creating array keys, rows of database tables, numbering actions, counting events.







2024 gtavrl.ru.