php - 如何使用 Validation Laravel 排除字符串中的电话号码和 URL?
问题描述
我想验证一个不包含电话号码或网址的“标题”字段。我已经尝试过使用正则表达式,它只有在我单独输入电话号码或 url 时才有效
举个例子:
0617859654 --> 工作,验证失败
但
测试 0617859654 -> 不工作,验证未检测到电话号码
这是我的代码:
$validator = Validator::make($request->all(), [
'titre' => 'not_regex:/^([0-9\s\-\+\(\)]*)$/|not_regex:/^https:\/\/\w+(\.\w+)*(:[0-9]+)?\/?$/|not_regex:/^http:\/\/\w+(\.\w+)*(:[0-9]+)?\/?$/',
]);
因此验证不能完全起作用,如果电话号码或 url 在我的标题字段中,我如何阻止验证?
谢谢 !
解决方案
我解决这个问题的方法是在给定的字符串中找到匹配的模式。我没有确切的解决方案,但 PHP 有一个名为Preg_match()或preg_match_all()的方法
首先在这些方法中传递您的标题,它将返回 True 或 false 甚至匹配的模式。
preg_match ( string $pattern , string $subject , array &$matches = null , int $flags = 0 , int $offset = 0 ) : int|false
或者
preg_match_all ( string $pattern , string $subject , array &$matches = null , int $flags = 0 , int $offset = 0 ) : int|false|null
阅读更多:https ://www.php.net/manual/en/function.preg-match.php
希望你能明白。谢谢
推荐阅读
- sql - 将SqlServer表数据加载到csv文件的Shell脚本
- c++ - 动态分配包含要动态分配的向量的向量
- python - 如何递归地创建元组列表?
- google-apps-script - 将 Google Apps 脚本项目切换到 V8 运行时时的范围问题
- c++ - 为什么 std::size 的结果不是编译时间或不是 size_t?
- java - 循环遍历 ArrayList
- android - 如何在 MediaMuxer android 上更改分辨率
- redis - 如何为 CKAN (dockerized) 配置 ckanext-archiver 插件以使其工作?
- apache-spark - hive 如何使用条件语句根据结果执行不同的查询
- c# - 如何计算水晶报表Top 6科目的平均分?