本文的目的是介绍使用preg_match检测网址?字符串中不含http://的详细情况,我们将通过专业的研究、有关数据的分析等多种方式,同时也不会遗漏关于(PHP)正则表达式-preg_match和p
本文的目的是介绍使用preg_match检测网址?字符串中不含http://的详细情况,我们将通过专业的研究、有关数据的分析等多种方式,同时也不会遗漏关于(PHP)正则表达式-preg_match和preg_match_all()的用法、PHP preg_match 1 字符串 2 位验证、php preg_match_all 与preg_match 区别与实例、php preg_match与preg_match_all 函数区别分析的知识。
本文目录一览:- 使用preg_match检测网址?字符串中不含http://
- (PHP)正则表达式-preg_match和preg_match_all()的用法
- PHP preg_match 1 字符串 2 位验证
- php preg_match_all 与preg_match 区别与实例
- php preg_match与preg_match_all 函数区别分析
使用preg_match检测网址?字符串中不含http://
我想知道如何根据preg_match检查分成数组的字符串,看看它是否以www开头。我已经有一个检查http:// www的了。
function isValidURL($url){return preg_match(''|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i'', $url);}$stringToArray = explode(" ",$_POST[''text'']); foreach($stringToArray as $key=>$val){ $urlvalid = isValidURL($val); if($urlvalid){ $_SESSION["messages"][] = "NO URLS ALLOWED!"; header("Location: http://www.domain.com/post/id/".$_POST[''postID'']); exit(); } }
谢谢!斯特凡
答案1
小编典典您想要类似的东西:
%^((https?://)|(www\.))([a-z0-9-].?)+(:[0-9]+)?(/.*)?$%i
这是使用| 匹配http://
或www
开头。我更改了定界符,%
以避免与|
(PHP)正则表达式-preg_match和preg_match_all()的用法
<?php /** * 正则表达式练习 * User: Ollydebug * Date: 2015/11/13 * Time: 13:28 */ /* * preg_match()第三个参数可选,第三个参数是引用传递,它在匹配subject的时候,只会匹配一次 * preg_match_all()第三个参数必填,第三个参数也是引用传递,它在匹配subject的时候,会把所有满足条件的结果都匹配出来 */ $pattern = '/[0-9]/'; $subject = 'weuyr3ui76as83s0ck9'; $m1 = $m2 = array(); $t1 = preg_match($pattern,$subject,$m1); $t2 = preg_match_all($pattern,$subject,$m2); show($m1); echo '<hr/>'; show($m2); echo '<hr/>'; show($t1.'||'.$t2); function show($var){ if(empty($var)){ echo 'null'; }elseif(is_array($var)||is_object($var)){ // array,object echo '<pre>'; print_r($var); echo '
以上就是(php)正则表达式-preg_match和preg_match_all()的用法的内容,更多相关内容请关注php中文网(www.php.cn)!
PHP preg_match 1 字符串 2 位验证
如何解决PHP preg_match 1 字符串 2 位验证?
值:a11b22c33
我想用带有 1 个字符串和 2 个数字的 preg_match 验证上述值
preg_match(''/^\s{1}\d{2}\s{1}\d{2}\s{1}\d{2}$/'',$value)
我试过了,但没有用。
解决方法
使用这个:
preg_match(''/^([a-z]\d{2}){3}$/'',$value)
\s
匹配空格,[a-z]
匹配小写字母。如果您还需要大写字母,请在第二个 i
后添加 /
修饰符,使其不区分大小写。
由于您重复了 3 次相同的模式,因此我将其分组并使用了 {3}
量词,而不是将其写出 3 次。
php preg_match_all 与preg_match 区别与实例
/*
代码如下 | 复制代码 |
int preg_match_all ( string $pattern , string $subject , array &$matches [, int $flags [, int $offset ]] ); |
搜索所有匹配正则表达式的模式并提出给予他们在比赛中受的标志指定的顺序。第一场比赛后发现,随后的搜查是继续从最后一场比赛结束。
实例
代码如下 | 复制代码 |
preg_match_all("|]+>(.*)[^>]+>|u", "example: this is a test ",$out, preg_pattern_order); echo $out[0][0] . ", " . $out[0][1] . " "; echo $out[1][0] . ", " . $out[1][1] . " "; |
出输
代码如下 | 复制代码 |
example: , this is a test
example: , this is a test
this is a test ",$out, preg_set_order); echo $out[0][0] . ", " . $out[0][1] . " "; echo $out[1][0] . ", " . $out[1][1] . " ";
立即学习“PHP免费学习笔记(深入)”; int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags [, int $offset ]]] )
) |
*/
php preg_match与preg_match_all 函数区别分析
正则表达式在 PHP 中的应用
在 PHP 应用中,正则表达式主要用于:
•正则匹配:根据正则表达式匹配相应的内容
•正则替换:根据正则表达式匹配内容并替换
•正则分割:根据正则表达式分割字符串
在 PHP 中有两类正则表达式函数,一类是 Perl 兼容正则表达式函数,一类是 POSIX 扩展正则表达式函数。二者差别不大,而且推荐使用Perl 兼容正则表达式函数,因此下文都是以 Perl 兼容正则表达式函数为例子说明。
定界符
Perl 兼容模式的正则表达式函数,其正则表达式需要写在定界符中。任何不是字母、数字或反斜线()的字符都可以作为定界符,通常我们使用 / 作为定界符。具体使用见下面的例子。
提示
尽管正则表达式功能非常强大,但如果用普通字符串处理函数能完成的,就尽量不要用正则表达式函数,因为正则表达式效率会低得多。关于普通字符串处理函数。
preg_match()
preg_match() 函数用于进行正则表达式匹配,成功返回 1 ,否则返回 0 。
语法:
int preg_match( string pattern,string subject [,array matches ] )
参数说明:
参数
说明
pattern
正则表达式
subject
需要匹配检索的对象
matches
可选,存储匹配结果的数组, $matches[0] 将包含与整个模式匹配的文本,$matches[1] 将包含与第一个捕获的括号中的子模式所匹配的文本,以此类推
例子 1 :