当前位置:首页 > Web开发 > 正文

PHP正则表达式及表单注册案例

2024-03-31 Web开发

正则表达式是一种具有特定模式的用来匹配文本的字符串

preg_match 匹配

$pattern = ‘/php/‘; $subject = "php 是最好的编程语言,php 没有之一!"; $result = preg_match($pattern,$subject); if($result){ echo "<h1>匹配告成</h1>"; }else{ echo "<h1>匹配弗成功</h1>"; }

preg_match_all 匹配所有

$pattern = ‘/php/‘; $subject = "php是最好的编程语言,不会php的措施员不是好措施员!"; $result = preg_match_all($pattern,$subject,$matches); var_dump($result); var_dump($matches); //preg_match=== //int 1 //array (size=1) // 0 => string ‘php‘ (length=3) //preg_match_all ==== //int 2 //array (size=1) // 0 => // array (size=2) // 0 => string ‘php‘ (length=3) // 1 => string ‘php‘ (length=3)

preg_replace 正则替换

preg_filter() 等价于 preg_replace() ,,但它仅仅返回与方针匹配的功效

$pattern = ‘/www\..*\.com/‘; $replacement = ‘‘; $subject = [ ‘‘, ‘‘, ‘coding.test.com‘ ]; $result = preg_replace($pattern,$replacement,$subject); var_dump($result); //array(3) { // [0] => // string(12) "www.test.com" // [1] => // string(12) "www.test.com" // [2] => // string(15) "coding.test.com" //} $result1 = preg_filter($pattern,$replacement,$subject); var_dump($result1); //array(2) { // [0] => // string(12) "www.test.com" // [1] => // string(12) "www.test.com" //}

正则搜索并使用回调替换

$pattern = ‘/(.*)(\d{4})(-\d{2}-\d{2})/‘; $subject = "今天是2020-01-14"; $result = preg_replace_callback($pattern,‘nextyear‘,$subject); var_dump($result); function nextyear($vars){ return ‘明年的‘.$vars[1].($vars[2]+1).$vars[3];//string(28) "明年的今天是2021-01-14" } //array (size=4) // 0 => string ‘今天是2020-01-14‘ (length=19) // 1 => string ‘今天是‘ (length=9) // 2 => string ‘2020‘ (length=4) // 3 => string ‘-01-14‘ (length=6)

PREG_GREP_INVERT 返回没有被匹配替换到的

$pattern = ‘/www\..*\.com/‘; $subject = [ ‘‘, ‘‘, ‘coding.imooc.com‘ ]; $result = preg_grep($pattern,$subject,PREG_GREP_INVERT); var_dump($result);//string(16) "coding.imooc.com"

preg_split 凭据指定法则支解字符串转数组

$pattern = ‘/\||,|\s/‘; $replacement = ‘|‘; $subject = "php|asp,jsp html"; $result1 = preg_split($pattern,$subject,3); var_dump($result1); //array (size=4) // 0 => string ‘php‘ (length=3) // 1 => string ‘asp‘ (length=3) // 2 => string ‘jsp‘ (length=3) // 3 => string ‘html‘ (length=4)

转义正则表达式字符

$str = ‘<a href="http://www.test.com">测试网</a>‘; $result = preg_quote($str,‘/‘); $pattern = ‘/‘.$result.‘/‘; $subject = ‘<a href="http://www.test.com">测试网</a>‘; echo preg_match($pattern,$subject); //\<a href\="http://www\.test\.com"\>测试网\</a\>

()单独获取

$pattern = ‘/[\da-z]+([\._\-]*[\da-z]+)*@[\da-z]+([\.\-][\da-z]+)*\.[a-z]+/i‘; $subject = <<<php //[email protected] //[email protected] //[email protected] //[email protected] //[email protected] //[email protected] php; preg_match_all($pattern,$subject,$matches); var_dump($matches); //array(3) { // [0] =>//打印出所有匹配到的 // array(6) { // [0] => // string(15) "[email protected]" // [1] => // string(16) "[email protected]" // [2] => // string(19) "[email protected]" // [3] => // string(17) "[email protected]" // [4] => // string(18) "[email protected]" // [5] => // string(19) "[email protected]" // } // [1] =>//打印出第一个()里的 // array(6) { // [0] => // string(0) "" // [1] => // string(4) ".123" // [2] => // string(4) "_123" // [3] => // string(5) "--123" // [4] => // string(0) "" // [5] => // string(0) "" // } // [2] =>//打印出第二个()里的 // array(6) { // [0] => // string(0) "" // [1] => // string(0) "" // [2] => // string(0) "" // [3] => // string(0) "" // [4] => // string(4) ".com" // [5] => // string(3) "-qq" // } //}

ajax注册表单案例

技术图片

index.html

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/31600.html