在php中读取筛配置文件



我正在迁移我们的邮件平台,

我为每个用户创建了一个文件(我没有访问Sieve主机的权限)

看起来像这样

require "vacation";
if not header :contains "Precedence" ["bulk","list"] {
vacation
:days 15
:addresses ["some@email.tld", "some@email.tld"]
:subject "Out of office Subject"
"Some out of office tekst
With multiple lines 
another line"
;}

我想在PHP中获得主题和消息作为每个变量。如何才能做到这一点呢?

不使用正则表达式的解决方案如下:

<?php
$input = file_get_contents("input");
$needle = ":subject";
$theString = substr($input, strpos($input, $needle) + strlen($needle));
$newLinePosition = strpos($theString, "n");
$subject = substr($theString, 0, $newLinePosition);
$content = substr($theString, $newLinePosition + 1);
echo "SUBJECT IS n" . $subject . "n";
echo "CONTENT IS n" . $content . "n";

解释:

  • 我们将文件的内容加载到$input(当然,您可以循环一个文件名数组)
  • 我们定义我们的针,它是:subject,我们的有用内容从这个指针的末尾开始,直到字符串
  • 的末尾。
  • 我们提取有用的内容并将其存储在$theString
  • 我们在有用的内容中找到第一个换行符的位置,知道主题在它之前,内容在它之后
  • 我们将主题和内容提取到各自的变量
  • 我们输出这些值

您可以使用preg_match执行正则表达式匹配:

<?php
$script = file_get_contents('./file.sieve');
// Regular expression to match the subject and message lines
$pattern = '/:subject "(.+)"/';
// Use preg_match to apply the regular expression and extract the subject and message
preg_match($pattern, $script, $matches);
// The subject is in the first capture group, and the message is in the second capture group
$subject = trim(str_replace(':subject', '', $matches[0]));
$message = $matches[1];
print_r($subject);
print_r("n");
print_r($message);

最新更新