Home > Article > Backend Development > What are atoms in regular expressions in PHP? What content does it consist of? (with code)
The previous article introduced you to "What is the delimiter of a regular expression? What do we need to pay attention to? (Attached code) 》, this article continues to introduce to you what are atoms in regular expressions in PHP? What content does it consist of? (With code)
##Atoms in regular expressions
What is an atom:
The smallest unit that makes up a regular expression is an atom.What are the contents of atoms:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>表单页面</title> </head> <body> <form action= "2.php"method= "post"> <input type="text" name="str" id=""/> <input type="submit" value="匹配" > </form> </body> </html>Then we enter a ( submit) for matching. After matching, it will be passed to the page we just created. At this time, we are creating a new page. In the new page, do we need to match? What we need to do is those atoms whose content is our regular. Before matching, we first define a string to receive, and then we define a regular expression, because if the regular expression wants to match, it must cooperate with the function, so we have to call the regular matching function (preg_match( )), at the beginning we passed in two parameters, one is the regular expression we defined, and the second is the string we want to match. In fact, we can pass the third parameter which is the matched result (match ), if it matches, it returns 1, if it does not match, it returns 0; so we output the matching result, call the if statement, and judge the output result The code is as follows:
<?php //进行匹配 $str = $_POST['str']; $pattern = '//' ; //调用正则匹配函数 $result = preg_match($pattern , $str , $match); echo ' 匹配结果为: '.$result; echo '<hr/>' ; if ( $result){ echo '<font color="pink" size="5 ">匹配成功</font>'; }else{ echo '<font color="red">匹配失败</font>'; } var_dump($match); ?>The code result is as follows: When we put $pattern = '//', add a b, ($pattern = '/b/'), see if it is atomic, and then we run It was found that the match was successful; The code result is as follows: It can be seen from the above code running result that b is an atom, so By analogy, we can still get successful matching results when we test B;
PHP Video Tutorial"
The above is the detailed content of What are atoms in regular expressions in PHP? What content does it consist of? (with code). For more information, please follow other related articles on the PHP Chinese website!