Home > Article > Backend Development > How to detect if a string only contains numbers in php
Two methods: 1. Use is_numeric() to detect whether a string is a numeric string, the syntax is "is_numeric (string)", if TRUE is returned, it only contains numbers. 2. Use preg_replace() with regular expressions to filter characters, return numeric characters, and form a numeric string. Use "===" to compare whether the numeric string and the original string are equal. If they are equal, they only contain digits. The syntax "preg_replace( "/[^0-9]/","",string)====string".
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
php detection string Two methods to determine whether it contains only numbers:
Method 1: Use the is_numeric() function to detect if the
string contains only numbers, then the String is a numeric string.
The is_numeric() function can detect whether a string is a numeric string.
is_numeric ($var)
$var
: Variable to be detected.
Returns TRUE if the specified variable $var
is a number or a string of numbers, otherwise returns FALSE
<?php header("Content-type:text/html;charset=utf-8"); $str1="a678"; $str2="678"; $str3="3.14"; if (is_numeric($str1)){ echo "$var_name1 是数字字符串<br><br>"; } else{ echo "$str1 不是数字字符串<br><br>" ; } if (is_numeric($str2)){ echo "$str2 是数字字符串<br><br>"; } else{ echo "$str2 不是数字字符串<br><br>"; } ?>
Method 2: Use the preg_replace() function and the "===" operator to detect
Use the preg_replace() function with regular expressions to filter characters , returns numeric characters to form a numeric string
Use the "===" operator to compare whether the numeric string and the original string are equal. If they are equal, the string only contains digits
<?php header("Content-type:text/html;charset=utf-8"); function f($str){ $result = preg_replace("/[^0-9]/", "", $str); if($result===$str){ echo "$str 字符串中只含数字<br><br>"; } else{ echo "$str 字符串中还有其他字符<br><br>" ; } } f("a678"); f("678"); ?>
preg_replace("/[^0-9]/", "", $str)
means matching except 0~9
characters other than numbers between them, and replace these characters with null characters, that is, delete these characters. Then only numeric characters are left.
Description: The preg_replace function performs a regular expression search and replacement. It is a powerful string replacement processing function. Its syntax format is
preg_replace($pattern, $replacement, $subject [, $limit = -1 [, &$count]])
Search for the part matching pattern in subject and replace it with replacement.
Parameter description:
$pattern: The pattern to be searched, which can be a string or a string array.
$replacement: String or array of strings used for replacement.
$subject: The target string or string array to be searched and replaced.
$limit: Optional, the maximum number of substitutions for each subject string per pattern. The default is -1 (no limit).
$count: Optional, the number of times the replacement is performed.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to detect if a string only contains numbers in php. For more information, please follow other related articles on the PHP Chinese website!