gpt4 book ai didi

php - 我怎样才能让我的 php 代码更短?

转载 作者:行者123 更新时间:2023-12-04 06:34:54 26 4
gpt4 key购买 nike

有人可以就我如何使这段代码变得更短给我建议吗?

它的作用是从上传到服务器的文本文件中获取数据。它读取文件并查找 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";
}

任何帮助将真的是有帮助的家伙!

最佳答案

首先,这段代码以几乎完全相同的方式编写了 3 次:

// 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";
}

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

关于php - 我怎样才能让我的 php 代码更短?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4996127/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com