检查字符串在PHP中是否只有用逗号分隔的整数 [英] Check if String has only Integers separated by comma in PHP

查看:32
本文介绍了检查字符串在PHP中是否只有用逗号分隔的整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Regex 方面真的很空,这就是为什么不知道如何在 PHP 中创建一个 Regex 来检查字符串是否具有这个特定的字符序列.

I am really empty at Regex side and that's why couldn't get how to make a Regex in PHP which checks if the string has this particular sequence of characters.

$str = '2323,321,329,34938,23123,54545,123123,312312';

意思是检查字符串是否只包含整数(不是十进制,没有字母或其他任何东西),以逗号 (,) 分隔.

Means to check if the string contains only Integers (Not decimal, no alphabats or anything else) separated by comma (,).

推荐答案

你可以使用这个正则表达式:

You can use this regex:

'/^\d+(?:,\d+)*$/'

代码:

$re = '/^\d+(?:,\d+)*$/';
$str = '2323,321,329,34938,23123,54545,123123,312312'; 

if ( preg_match($re, $str) )
    echo "correct format";
else
    echo "incorrect format";

这篇关于检查字符串在PHP中是否只有用逗号分隔的整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆