Home >Backend Development >PHP Problem >How to convert php string into numerical value

How to convert php string into numerical value

PHPz
PHPzOriginal
2023-04-04 10:42:02999browse

PHP is a popular server-side programming language that is widely used for web development and data processing. It has a rich set of built-in functions and features. When we process user input or get data from external sources, we often need to convert strings into numeric values ​​for calculation and comparison operations. This article will introduce methods and techniques for converting PHP strings into numerical values, including type conversion functions, string manipulation functions and regular expressions. let's start!

  1. PHP type conversion function

PHP provides several built-in type conversion functions for converting values ​​of different types into numeric types. These functions can handle different data types such as integers, floating point numbers, Boolean values, and datetimes. The following are some commonly used type conversion functions:

(1) intval() function

intval() function is used to convert strings to integers. If the string begins with a number, the function converts it to an integer type; if the string begins with a non-numeric character, the function returns 0. The intval() function can also specify the base and round off the decimal part.

For example:

$str = '123';
$int = intval($str); // $int的值为123
$str = 'abc123';
$int = intval($str); // $int的值为0
$str = '123.45';
$int = intval($str); // $int的值为123
$str = '0x1a';
$int = intval($str, 16); // $int的值为26

(2) floatval() function

The floatval() function is used to convert a string to a floating point number. Similar to the intval() function, if the string starts with a number, the function will convert it to a floating point number type; if the string starts with a non-numeric character, the function returns 0.

For example:

$str = '3.14';
$float = floatval($str); // $float的值为3.14
$str = 'abc.123';
$float = floatval($str); // $float的值为0

(3) boolval() function

boolval() function is used to convert a string into a Boolean value. If the string is '0', '', 'false', 'null' or an empty array, the function returns false; otherwise it returns true. The boolval() function can also convert other types of values ​​to Boolean values.

For example:

$str1 = '0';
$str2 = 'false';
$str3 = 'null';
$bool1 = boolval($str1); // $bool1的值为false
$bool2 = boolval($str2); // $bool2的值为false
$bool3 = boolval($str3); // $bool3的值为false
$str4 = 'abc';
$bool4 = boolval($str4); // $bool4的值为true
$int = 123;
$bool5 = boolval($int); // $bool5的值为true

(4) strtotime() function

The strtotime() function is used to convert date and time strings into Unix timestamps. The Unix timestamp is the number of seconds from 0:00:00 on January 1, 1970 to the current time. The parameters of the function can be date and time strings in any format, such as '2019-01-01', '2020-01-01 12:00:00', etc. If the string is not recognized as a datetime format, the function returns false.

For example:

$str = '2020-01-01 12:00:00';
$timestamp = strtotime($str); // $timestamp的值为1577875200
  1. PHP string operation function

In addition to type conversion functions, PHP also provides many string operation functions for processing The numeric part of the string. These functions can implement functions such as removing non-numeric parts of a string, intercepting numbers of a specified length, and determining whether a string is a number. The following are some commonly used string manipulation functions:

(1) preg_replace() function

preg_replace() function is a string replacement function based on regular expressions in PHP, which can be used to remove The non-numeric part of the string. We can use regular expressions to match all non-numeric characters and replace them with nothing.

For example:

$str = 'abc123xyz456';
$str = preg_replace('/\D/', '', $str); // $str的值为'123456'

In regular expressions, '\D' represents non-numeric characters, and adding '/' means escaping them. Use the preg_replace() function and regular expressions to replace non-numeric characters with an empty string to get a string containing only numbers.

(2) substr() function

The substr() function is used to intercept the substring at the specified position and length in the string. We can use this function to intercept the numeric part of the string to perform type conversion.

For example:

$str = 'abc123xyz';
$str = substr($str, 3, 3); // $str的值为'123'
$int = intval($str); // $int的值为123

(3) is_numeric() function

The is_numeric() function is used to determine whether a string is a number. The function returns true if the string is an integer, a floating point number, or a number in scientific notation; otherwise it returns false.

For example:

$str1 = '123';
$str2 = '3.14';
$str3 = '1e10';
$str4 = 'abc';
$bool1 = is_numeric($str1); // $bool1的值为true
$bool2 = is_numeric($str2); // $bool2的值为true
$bool3 = is_numeric($str3); // $bool3的值为true
$bool4 = is_numeric($str4); // $bool4的值为false
  1. PHP Regular Expression

Regular expression is a flexible and powerful text matching tool that can be used to process various A string in a format. PHP has a built-in PCRE library that supports Perl-compatible regular expression syntax. We can use regular expressions to match the numeric part of the string and perform type conversion.

For example:

$str1 = 'abc123xyz';
$str2 = '3.14E2';
$pattern = '/\d+/';
preg_match($pattern, $str1, $matches); // $matches的值为array('123')
preg_match($pattern, $str2, $matches); // $matches的值为array('314')
$int1 = intval($matches[0]); // $int1的值为123
$float1 = floatval($matches[0]); // $float1的值为123.0
$float2 = floatval($matches[0]) * 100; // $float2的值为12300.0

In regular expressions, '\d' represents a numeric character, and ' ' represents matching one or more numeric characters. We use the preg_match() function and regular expressions to match the numeric part of the string, and store the matching results in the $matches array; then use the intval() function and floatval() function to convert the matching results into integer and floating-point number types. In addition, we can further process the matching results, such as multiplying by 100 to obtain a floating point number.

Summary

This article introduces the methods and techniques for converting PHP strings into numerical values, including type conversion functions, string manipulation functions and regular expressions. These methods can easily handle various types of strings such as integers, floats, booleans, datetimes, etc. Finally, we also introduced how to use regular expressions to match the numeric part of a string and perform type conversion. Mastering these skills can make you more comfortable in the PHP programming process and improve development efficiency.

The above is the detailed content of How to convert php string into numerical value. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn