【问题标题】:explode string using two delimiters and loop through result使用两个分隔符分解字符串并遍历结果
【发布时间】:2020-04-24 18:45:33
【问题描述】:

在这个网站上搜索了两天,我找不到合适的方法来解决我的请求,所以请指导我。

我有一个字符串,它可能包含插入到字符串中不同位置的几对分隔符,例如:

敏捷的[begin]棕狐[end]跳过[begin]懒惰的[end]狗。

[begin]快速的棕色[end]狐狸跳过[begin]懒惰[end]的狗。

我需要什么:每次找到这些分隔符时,我都想在 pdf 的两列中显示分隔内容,但还要在这些分隔符之前和之后保留字符串的其余部分。

假设上面的第一个例子我需要得到以下结果:

快的 $mpdf->SetColumns(2); 棕狐 $mpdf->SetColumns(0); 跳过 $mpdf->SetColumns(2); 懒人 $mpdf->SetColumns(0); 狗

这是我从 php.net 使用的函数,但正如您所见,它仅适用于一对分隔符,我不知道如何在这里使用 foreach 并保持我需要的格式:

    function multiexplode ($delimiters,$string) {
    $ready = str_replace($delimiters, $delimiters[0], $string);
    $launch = explode($delimiters[0], $ready);
    return  $launch;
}

$exploded = multiexplode(array('[begin]','[end]'),$mystring);

//if delimiters exist, then split in two columns only those parts of the string
if (count($exploded) > 1) {

//begining of the string
$mpdf->WriteHTML($exploded[0]);

//split in two columns
$mpdf->SetColumns(2);
$mpdf->WriteHTML($exploded[1]);

//reset to single column and write the rest of the string
$mpdf->SetColumns(0);
$mpdf->WriteHTML($exploded[2]);

} else {
//set two columns for the entire string
$mpdf->SetColumns(2);
$mpdf->WriteHTML($mystring);
}

谢谢

【问题讨论】:

    标签: php explode mpdf


    【解决方案1】:

    我想我找到了解决我的请求的方法。向下滚动到 php.net 的同一页面,我发现此功能可用:

    function multiexplode ($delimiters,$string) {
    $ary = explode($delimiters[0],$string);
    array_shift($delimiters);
    if($delimiters != NULL) {
        foreach($ary as $key => $val) {
             $ary[$key] = multiexplode($delimiters, $val);
        }
    }
    return  $ary;
    

    }

    使用这个函数和来自 Andrew 的提示(谢谢!)我的代码现在看起来像这样:

                $exploded = multiexplode(array('[begin]','[end]'),$mystring);
    
            //if delimiters exist, then split in two columns only those parts of the string
            if (count($exploded) > 1) {
    
            //slice the array from begining
            foreach(array_slice($exploded,0) as $key=>$explode) {
                //show the first slice of the array
                if ($key == 0) {
                $mpdf->SetColumns(0);
                $mpdf->WriteHTML($explode[0]);
                //get the rest of sliced array and set it to 2 columns
                }   else if ($key > 0) {
                $mpdf->SetColumns(2);
                $mpdf->WriteHTML($explode[0]);
                //if delimiters [end], then reset columns to 0 and continue with the rest of the sliced array
                $mpdf->SetColumns(0);
                $mpdf->WriteHTML($explode[1]);
                }
            }
    
            } else {
            //set 2 columns to entire string
            $mpdf->SetColumns(2);
            $mpdf->WriteHTML($mystring);
            }
    

    我相信还有其他更好的选择,所以请随时添加您的代码。谢谢你。

    【讨论】:

      【解决方案2】:

      查看preg_split。它类似于explode,但您可以使用正则表达式作为分隔符。

      https://www.php.net/manual/en/function.preg-split.php

      下面是一个示例,使用 [begin] 或 [end] 作为分隔符: https://3v4l.org/nlC9K

      代码:

      $string = 'The quick [begin]brown fox[end] jumps over [begin]the lazy[end] dog.';
      
      $array = preg_split('/\\[begin\\]|\\[end\\]/', $string, -1);
      
      print_r($array);
      

      输出:

      Array
      (
          [0] => The quick 
          [1] => brown fox
          [2] =>  jumps over 
          [3] => the lazy
          [4] =>  dog.
      )
      

      您可以使用trim 删除多余的空格。

      更新:基于您的示例的代码:

      $exploded = preg_split('/\\[begin\\]|\\[end\\]/', $mystring, -1);
      
      // Iterate through the array
      foreach($exploded as $key => $string) {
          $mpdf->WriteHTML(trim($string));
          if($key++ % 2 == 0) {
              // Every 2nd one
              $mpdf->SetColumns(0);
          } else {
              // Every odd one
              $mpdf->SetColumns(2);
          }
      }
      

      再次,我不确定 SetColumns 应该实现什么 - 如果数组中只有一个元素,您可能需要以不同的方式处理它,也许您需要在最后一个元素之后再关闭它。但这应该是一个好的开始。

      【讨论】:

      • 请在答案中包含代码,因为将来外部引用可能会失效。
      • 谢谢你,安德鲁,你能告诉我如何在我的请求中使用你的例子吗?
      • 我不是 100% 确定我理解您想要实现的目标,我绝对不熟悉 mpdf 以及 setColumns 应该做什么。关键是您将需要使用 foreach 来遍历数组。我会尽快用一个例子更新我的答案,但我不确定它会按照你想要的方式工作。
      • @Mircea T 我更新了我的答案。同样,它可能无法完美实现您的预​​期,但您可以根据自己的需要进行修改。
      • 谢谢,我会试试的。为了给我的问题提供更多信息,让我们这样提出问题:回显整个字符串,每次找到像 [begin]text[end] 这样的一对分隔符时,写“text - OK”,然后继续字符串的其余部分.. 在我的情况下,我应该添加 $mpdf->setColumns(2) 而不是“text-ok”,以便在 pdf 的 2 列中显示分隔文本
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-06
      • 1970-01-01
      • 1970-01-01
      • 2015-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多