Case description:
Find all characters in the text file (input.txt) that meet the following conditions
1. It is a lowercase letter, and
2. There are only three uppercase letters on each side (consider newlines) ) For example, b in xAAAbAAAd
First create input.txt
in the same level directory, and then type some English letters in the file (can be regular or irregular)
For example:
asadfsaxAAAbAAAdRYTaASD
The code is as follows:
<?php header('content-type:text/html;charset=utf8 '); function getLower($data){ $length = strlen($data)-1; $str = ''; for ($i=0;$i<$length;$i++){ $flag = true; if(!isUpper($data[$i])){//当前为小写 成立 if($i == 3){ $flag = getFlag($data,$i); //如果后第四个是大写 不成立 if(isUpper($data[$i+4])){ $flag = false; } }else if($i == $length-3){ $flag = getFlag($data,$i); //如果前第四个是大写 不成立 if(isUpper($data[$i-4])){ $flag = false; } }else if($i>3 && $i<$length-3){ $flag = getFlag($data,$i); //如果前||后第四个有一个是大写就不成立 if(isUpper($data[$i+4]) || isUpper($data[$i-4])){ $flag = false; } }else{ $flag = false; } if($flag){ @$str .= $data[$i]; } } } return $str; } //公共当前字符的前三后和三个 function getFlag($data,$i){ $flag = true; for($j=$i-3;$j<=$i+3;$j++){ if($j != $i){ //如果有一个是小写就不成立 if(!isUpper($data[$j])){ $flag = false; } } } return $flag; } /* 判断是否是大写字母*/ function isUpper($s){ if(@ord($s) < 97){ return true; }else{ return false; } } $res =''; $d=file_get_contents('input.txt'); $res = getlower(str_replace("\r\n","",$d)); echo $res;
bda
The above introduces PHP to determine the character type. PHP reads the matching condition string from the file, including the content of PHP to determine the character type. I hope it will be helpful to friends who are interested in PHP tutorials.