我试图创建一个搜索功能来搜索txt文件中包含的数据中的名称。
我有这样的数据:
class PythonOperator(BaseOperator):
template_fields = ('templates_dict', 'op_args', 'op_kwargs')
ui_color = '#ffefeb'
# since we won't mutate the arguments, we should just do the shallow copy
# there are some cases we can't deepcopy the objects(e.g protobuf).
shallow_copy_attrs = ('python_callable', 'op_kwargs',)
@apply_defaults
def __init__(
self,
python_callable, # type: Callable
op_args=None, # type: Optional[Iterable]
op_kwargs=None, # type: Optional[Dict]
provide_context=False, # type: bool
templates_dict=None, # type: Optional[Dict]
templates_exts=None, # type: Optional[Iterable[str]]
*args,
**kwargs
):
我想到了将数据更改为如下数组的想法:
dimitri, 1998, php
nikolai, 1998, php
yuri, 1998, php
alyosha, 1998, php
然后更多地划分为多维
Array
(
[0] => dimitri, 1998, php
[1] => nikolai, 1998, php
[2] => yuri, 1998, php
[3] => alyosha, 1998, php
)
这样我就可以通过键搜索名称。
现在我不知道该怎么办了。我尝试使用Array
(
[0] => dimitri
Array(
[0] => 1998
[1]=> php
[1] => nikolai
Array(
[0] => 1998
[1]=> php
[2] => yuri
Array(
[0] => 1998
[1]=> php
[3] => alyosha
Array(
[0] => 1998
[1]=> php
)
函数爆炸数组中的值,但是它不起作用,这又产生了另一个问题,数组仅显示一些字符。
foreach()
$array = array();
$split = explode('\n', file_get_contents($file));
foreach ($split as $content){
$array = array_filter(array_map("trim", explode("\n", $content)));
$array1 = array();
$split2 = explode(", ", $array);
foreach($array as $row){
$array1[$row[1]][$row[2]][]=$row[0];
}
}
关于我的搜索,我正在考虑使用帖子,如果在搜索输入中正确键入数据,则将显示数据。
答案 0 :(得分:1)
以一个PHP示例为例,将显示多行匹配:
<?php
$file = 'somefile.txt';
$searchfor = 'name';
// the following line prevents the browser from parsing this as HTML.
header('Content-Type: text/plain');
// get the file contents, assuming the file to be readable (and exist)
$contents = file_get_contents($file);
// escape special characters in the query
$pattern = preg_quote($searchfor, '/');
// finalise the regular expression, matching the whole line
$pattern = "/^.*$pattern.*\$/m";
// search, and store all matching occurences in $matches
if(preg_match_all($pattern, $contents, $matches)){
echo "Found matches:\n";
echo implode("\n", $matches[0]);
}
else{
echo "No matches found";
}
答案 1 :(得分:0)
您可以尝试关注
/* read data from text file, line by line */
$arr = []; // file contents go into this array
$handle = fopen('test.txt', 'r');
if ($handle) {
while (($line = fgets($handle)) !== false) {
$arr[] = explode(',', $line);
}
fclose($handle);
} else {
echo 'error occured';
}
/* echo the form */
$form = <<<HEREDOC
<form action="#" method="POST">
<input name="search_function" type="text" placeholder="Search who you want">
<input type="submit" name="search" value="Search">
</form>
HEREDOC;
echo $form;
/* the search logic */
$found = false;
$count = 0; // count # of hits
if(isset($_POST['search'])) {
foreach($arr as $key => $value) {
foreach ($value as $key => $value) {
if ($value == $_POST['search_function']) {
$count++;
$found = true;
break 2;
}
}
}
}
/* result */
$message = ($found === true) ? "found " . $_POST['search_function'] . " (" . $count . ") occurrence." : 'nothing found.';
echo $message;
答案 2 :(得分:0)
您的文件似乎是CSV格式(或者至少是其内容),因此请考虑以下代码:
$datas = array_map('str_getcsv', file($pathToYourFile));
它将文件转换成数组。
https://www.php.net/manual/fr/function.file.php https://www.php.net/manual/fr/function.str-getcsv.php