Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php函数ShellFolder和and链接_Php_Regex_Function_Shell_Grep - Fatal编程技术网

php函数ShellFolder和and链接

php函数ShellFolder和and链接,php,regex,function,shell,grep,Php,Regex,Function,Shell,Grep,我找到了一个带有函数ShellFolder($source、$search1、$search2)的代码{$shellBefehl…withegrep。到目前为止效果很好。但现在我遇到了一个问题,因为搜索的文本文件通常包含相同的术语。现在我想将两个搜索术语相互链接,必须在文本文件中找到。这是我的代码: <?php function ShellFolder($source, $search1, $search2){ $shellBefehl = "egrep -o -w -l -a -

我找到了一个带有
函数ShellFolder($source、$search1、$search2)的代码{$shellBefehl…
with
egrep
。到目前为止效果很好。但现在我遇到了一个问题,因为搜索的文本文件通常包含相同的术语。现在我想将两个搜索术语相互链接,必须在文本文件中找到。这是我的代码:

<?php
function ShellFolder($source, $search1, $search2){
    $shellBefehl = "egrep -o -w -l -a --directories=recurse '$search1' '$search2' $source";
    exec($shellBefehl, $var);
    return $var;
}

//$source = 'grep.txt';
$source = './ordner/daten';
$search1 = $this->item->title;
$search2 = $extraField->value;
$var = ShellFolder($source, $search1, $search2);

$myResult = print_r($var, true);

$text = $myResult;
$text_ohne = substr($myResult, 19);
$insgesamt = $text_ohne;
$insgesamt_neu=substr($insgesamt,0,-3);
echo ($search1);
echo ($search2);
$lines = file("$insgesamt_neu");

foreach($lines as $line) {
    echo($line);
}
Grep
例如,可以通过
-E
选项将多个模式传递给Grep,但Grep将搜索任何模式

如果要使用逻辑AND连接搜索模式,则Grep不方便,因为它不支持逻辑AND。如果假定
pattern1.*pattern2
pattern2
之前,则可以使用类似
pattern1.*pattern2>的模式模拟AND:

$patterns = ['pattern1', 'pattern2'];
$dir = escapeshellarg($dir);
$pattern = escapeshellarg(implode('.*', $patterns));
$command = "egrep -o -w -l -a -r $pattern $dir";
exec($command, $output, $exit_status);
pattern1.*pattern2 | pattern2.*pattern1
用于任何订单。但对于一般情况,该模式是次优的。换句话说,Grep不适用于一般情况,您应该使用其他工具

AWK 有一个:
awk'/pattern1/&&&&/pattern2/&&&……'文件
。但是,awk只接受一个文件,您必须手动迭代目录并将命令应用于每个文件:

<?php
/**
 * Searches for lines matching all regexp patterns.
 *
 * @param string $dir Path to directory with text files
 * @param array $patterns AWK patterns without regexp markers ('/')
 * @return array Files matching all patterns
 * @throws InvalidArgumentException
 */
function grepDir($dir, array $patterns, callable $callback) {
  if (!$patterns) {
    throw new InvalidArgumentException("Invalid patterns");
  }

  // Build command as awk '/pattern1/ && /pattern2/ && ... path-to-file'
  $awk_script = '/' . implode('/ && /', $patterns) . '/';
  $awk_script = escapeshellarg($awk_script);
  $command_format = "awk $awk_script %s";

  try {
    $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));

    $it->rewind();
    while ($it->valid()) {
      if (!$it->isDot()) {
        $file_path = $it->key();
        $command = sprintf($command_format, $file_path);
        $output = null;
        exec($command, $output, $exit_status);

        if ($exit_status) {
          trigger_error("Command failed: $command");
          continue;
        }

        if ($output) {
          $callback($file_path, $output);
        }
      }

      $it->next();
    }
  } catch (Exception $e) {
    trigger_error($e->getMessage());
    return false;
  }
  return true;
}

$dir = '.';
$patterns = [ '456', '123' ];

grepDir($dir, $patterns, function ($file_path, array $output) {
  printf("File: %s\nLines:\n%s\n--------\n",
    $file_path, implode(PHP_EOL, $output));
});
PHP 上面的示例可以轻松转换为纯PHP解决方案(无需调用shell命令)。您可以逐行读取文件,并根据应用程序的逻辑使用
preg_match()
测试该行是否与模式匹配:

$patterns = ['456', '123'];

$file = 'file1'; // Replace with $it->key() in the example above
if (! $fp = fopen('file1', 'r')) {
  throw new RuntimeException("Failed to open file $file");
}

while ($line = fgets($fp)) {
  $matches = true;
  foreach ($patterns as $pattern) {
    // You might want to quote the pattern, if it isn't supposed to be
    // interpreted as a regular expression:
    // $pattern = preg_quote($pattern, '/');
    if (!preg_match("/{$pattern}/", $line)) {
      $matches = false;
      break;
    }
  }

  if ($matches) {
    echo "Line $line matches all patterns\n";
  }
}

fclose($fp);

@UweJansen,如果答案能解决问题,请接受
$patterns = ['456', '123'];

$file = 'file1'; // Replace with $it->key() in the example above
if (! $fp = fopen('file1', 'r')) {
  throw new RuntimeException("Failed to open file $file");
}

while ($line = fgets($fp)) {
  $matches = true;
  foreach ($patterns as $pattern) {
    // You might want to quote the pattern, if it isn't supposed to be
    // interpreted as a regular expression:
    // $pattern = preg_quote($pattern, '/');
    if (!preg_match("/{$pattern}/", $line)) {
      $matches = false;
      break;
    }
  }

  if ($matches) {
    echo "Line $line matches all patterns\n";
  }
}

fclose($fp);