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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
