Home > Backend Development > PHP Tutorial > PHP determines character type php reads matching condition string from file

PHP determines character type php reads matching condition string from file

WBOY
Release: 2016-07-28 08:29:57
Original
1241 people have browsed it

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
Copy after login

Create a php file

The code is as follows:

<?php 
header(&#39;content-type:text/html;charset=utf8 &#39;);

function getLower($data){
    $length = strlen($data)-1;
    $str = &#39;&#39;;
    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;
Copy after login

The effect is as follows (browser output):

bda
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template