如何缩短php代码?

如何缩短php代码?,php,Php,有没有人能给我一些建议,如何使这段代码大大缩短 它所做的是从上传到服务器的文本文件中获取数据。它读取文件并查找3个关键字。我将每个关键字放在一个数组中,然后它输出该关键字所在的整行。然后在foreach语句中,我循环遍历它并回显该行(为了调试目的,但完成后我不希望它回显该行,而只是进行计数),然后它对关键字的所有实例进行计数。最后我重复了发生的次数。我觉得这样做可以更有效率和更有组织 $sf->countLines()是指我创建的一个类,用于处理小操作,在本例中,我能够计算用户上传的文件中的行数

有没有人能给我一些建议,如何使这段代码大大缩短

它所做的是从上传到服务器的文本文件中获取数据。它读取文件并查找3个关键字。我将每个关键字放在一个数组中,然后它输出该关键字所在的整行。然后在foreach语句中,我循环遍历它并回显该行(为了调试目的,但完成后我不希望它回显该行,而只是进行计数),然后它对关键字的所有实例进行计数。最后我重复了发生的次数。我觉得这样做可以更有效率和更有组织

$sf->countLines()
是指我创建的一个类,用于处理小操作,在本例中,我能够计算用户上传的文件中的行数。最后我将编译我的脚本并从中生成一个类。但现在我正在学习和修补,看看如何才能顺利完成

$_ = NULL;
$msg = "<br />[ Your Script ".$handle->file_dst_name_body." has been uploaded ]<br />";
$file = $handle->file_dst_path.$handle->file_dst_name_body.'.'.$handle->file_dst_name_ext;
$open = fopen($file, 'r+');
$story = utf8_decode(fread($open, filesize($file)));

echo $msg.'<br />';

$sf = new TextFileReader($file);
$sf->seekLine(1); // Go to line 1

echo "Total number of lines: ".$sf->countLines()."<br />";
if(preg_match_all('/\s{68}(.*)/m', $story, $_))
{
    $unique_pages = array_unique(array_map(create_function('$a', 'return ucfirst(strtolower(trim($a)));'), $_[1]));
    echo "<span><strong>Pages: </strong>".count($unique_pages)."</span><br />";
}
$searchfor = array();
$searchfor[0] = 'INT.';
$searchfor[1] = 'EXT.';
$searchfor[2] = 'I/E.';

// get the file contents, assuming the file to be readable (and exist)
$contents = mb_convert_encoding(file_get_contents($file), "UTF-8", "auto");
// escape special characters in the query
$pattern = preg_quote($searchfor[0], '/');
// finalize the regular expression, matching the whole line
$pattern = "/^.*$pattern.*\$/m";

$list = "";
$numInt  = 0;
// search, and store all matching occurences in $matches
if(preg_match_all($pattern, $contents, $matches))
{
    $list = "<ol>";
    foreach($matches as $match) {
        $list .= "<li><div class='int'>".implode("</div></li><li><div class='int'>", $match)."</div></li>\n";
    }
    $numInt = count($matches[0]);
    $list .= "</ol>";
}else{
    $list = "No matches found";
}

// escape special characters in the query
$pattern2 = preg_quote($searchfor[1], '/');
// finalize the regular expression, matching the whole line
$pattern2 = "/^.*$pattern2.*\$/m";
$list2 = "";
$numExt  = 0;
// search, and store all matching occurences in $matches
if(preg_match_all($pattern2, $contents, $matches2))
{
    $list2 = "<ol>";
    foreach($matches2 as $match) {
        $list2 .= "<li><div class='int'>".implode("</div></li><li><div class='int'>", $match)."</div></li>";
    }
    $numExt = count($matches2[0]);
    $list2 .= "</ol>";
}else{
    $list2 = "No matches found";
}

// escape special characters in the query
$pattern3 = preg_quote($searchfor[2], '/');
// finalize the regular expression, matching the whole line
$pattern3 = "/^.*$pattern3.*\$/m";
$list3 = 0;
$numIe = 0;
// search, and store all matching occurences in $matches
if(preg_match_all($pattern3, $contents, $matches3))
{
    $list3 = "<ol>";
    foreach($matches3 as $match) {
        $list3 .= "<li><div class='int'>".implode("</div></li><li><div class='int'>", $match)."</div></li>\n";
    }
    $numIe = count($matches3[0]);
    $lis3t .= "</ol>";
}else{
    $list3 = "No matches found";
}
$\uU9=NULL;
$msg=“
[您的脚本“$handle->file_dst_name_body.”已上载]
”; $file=$handle->file_dst_path.$handle->file_dst_name_body.'.$handle->file_dst_name_ext; $open=fopen($file,'r+'); $story=utf8_解码(fread($open,filesize($file)); 回显$msg.“
”; $sf=新的TextFileReader($file); $sf->seekLine(1);//去1号线 echo“总行数:”.$sf->countLines().“
”; if(preg_match_all('/\s{68}(.*)/m',$story,$)) { $unique_pages=array_unique(array_map(创建_函数('$a',返回ucfirst(strtolower(trim($a));'),$[1]); 回声“页面:”。计数($unique_页面)。“
”; } $searchfor=array(); $searchfor[0]=“INT.”; $searchfor[1]=“分机”; $searchfor[2]=“I/E.”; //获取文件内容,假设文件可读(并且存在) $contents=mb_convert_编码(文件获取内容($file),“UTF-8”,“自动”); //在查询中转义特殊字符 $pattern=preg_quote($searchfor[0],'/'); //完成正则表达式,匹配整行 $pattern=“/^.*$pattern.*\$/m”; $list=“”; $numInt=0; //搜索,并将所有匹配事件存储在$matches中 if(preg_match_all($pattern、$contents、$matches)) { $list=“”; foreach($matches作为$match进行匹配){ $list.=“
  • ”。内爆(“
  • ”,$match)。“
  • \n”; } $numInt=count($matches[0]); $list.=“”; }否则{ $list=“未找到匹配项”; } //在查询中转义特殊字符 $pattern2=preg_quote($searchfor[1],'/'); //完成正则表达式,匹配整行 $pattern2=“/^.$pattern2.*\$/m”; $list2=“”; $numExt=0; //搜索,并将所有匹配事件存储在$matches中 if(preg_match_all($pattern2,$contents,$matches2)) { $list2=“”; foreach($matches2作为$match){ $list2.=“
  • ”。内爆(“
  • ”,$match)。“
  • ”; } $numExt=count($matches2[0]); $list2.=”; }否则{ $list2=“未找到匹配项”; } //在查询中转义特殊字符 $pattern3=preg_quote($searchfor[2],“/”); //完成正则表达式,匹配整行 $pattern3=“/^.$pattern3.*\$/m”; $list3=0; $numIe=0; //搜索,并将所有匹配事件存储在$matches中 if(preg_match_all($pattern3,$contents,$matches3)) { $list3=“”; foreach($matches3作为$match){ $list3.=“
  • ”。内爆(“
  • ”,$match)。“
  • \n”; } $numIe=count($matches3[0]); $lis3t.=”; }否则{ $list3=“未找到匹配项”; }

    任何帮助都将是真正的帮助

    对于初学者来说,这段代码几乎是以同样的方式编写了三次:

    // search, and store all matching occurences in $matches
    if (preg_match_all($pattern2, $contents, $matches2)) {
        $list2 = "<ol>";
        foreach($matches2 as $match) {
            $list2 .= "<li><div class='int'>".implode("</div></li><li><div class='int'>", $match)."</div></li>";
        }
        $numExt = count($matches2[0]);
        $list2 .= "</ol>";
    }
    else {
        $list2 = "No matches found";
    }
    
    //搜索并将所有匹配事件存储在$matches中
    if(preg_match_all($pattern2,$contents,$matches2)){
    $list2=“”;
    foreach($matches2作为$match){
    $list2.=“
  • ”。内爆(“
  • ”,$match)。“
  • ”; } $numExt=count($matches2[0]); $list2.=”; } 否则{ $list2=“未找到匹配项”; }

    您可以将其纳入自己的方法中,这样您就不会重复代码(并在所有这些地方分别维护代码)。

    用简单的句子描述代码的功能。然后为每一个句子做一个函数,并将逻辑提取到其中。如果发现自己在重复句子,请重用代码中的函数

    <>您也可以考虑将代码中的代理实体转换为对象,例如使用OOP。那将是一次重构:

    机械
    • 获取每种记录类型,并将其转换为具有访问器的哑数据对象
    • 如果您有一个关系数据库,那么将每个表转换为一个哑数据对象
    • 把所有的过程代码放到一个类中
    • 对每一个漫长的过程应用提取方法和相关的重构来分解它。分解这些过程时,请使用Move方法将每个过程移动到相应的哑数据类
    • 继续,直到从原始类中删除所有行为。如果原始类是纯过程类,那么删除它是非常令人满意的

    更短并不总是更快,如果你必须在更短和可读之间做出选择,那就选择可读。哦,哈哈,你是对的。我只是觉得我在重复我的foreach循环,让变量以$list、$list2、$list3为例来检索我的结果哈哈,这可能是我应该考虑的,因为当我读到我正在做的事情时,我发现自己在重复我自己,我将把它添加到我的textClass中,并在那里进行繁重的工作。谢谢