如何使用 preg_match 检查整个文本 [英] how to use preg_match to check whole text

查看:42
本文介绍了如何使用 preg_match 检查整个文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 preg_match 函数有问题.
我想根据模式检查整个文本,如果整个文本与模式匹配,则返回 true,如果与模式不匹配或部分匹配,则返回 false,但我无法使用 php preg_match 函数执行此操作.
例如,我使用 bellow func 来检查输入是否为数字.

Hi i have a problem with preg_match function.
i want to check whole text against pattern and return true if whole text matched with pattern and false for not matched or partially matched with pattern but i can't do this with php preg_match function.
for example i use bellow func to check if input is digit or no.

if(preg_match("/[\d]/","99ab") return true; else return false;

上面的代码返回true,但我想返回false.我想检查的其他正则表达式是用户的名字和姓氏,带有 preg_match.
和另一个用于电话号码的正则表达式,格式为 (000) 000-0000.
请帮我解决这个问题.

above code return true but i want to this return false. and other regex that i want to check is users first name and last name with preg_match.
and another regex for phone numbers with this format (000) 000-0000.
please help me to solve this problem.

推荐答案

需要用^表示字符串的开始,用$表示结束

You need to use ^ to indicate the start of the string and $ for the end

if (preg_match("/^[\d]+$/","99ab")) return true; 
else return false;

这篇关于如何使用 preg_match 检查整个文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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