【问题标题】:Exploding a string, only at the last occurrence of the explode match爆炸字符串,仅在爆炸匹配的最后一次出现
【发布时间】:2016-03-18 20:57:16
【问题描述】:

我正在尝试引爆一个字符串,但我需要它只在最后一个'and' 而不是每个'and' 上引爆。有没有办法做到这一点?

<?php

$string = "one and two and three and four and five";
$string = explode(" and ", $string);

print_r($string);

?>

结果:

数组([0] => 一[1] => 二[2] => 三[3] => 四[4] => 五)

需要结果:

数组([0] => 一二三四 [1] => 五)

【问题讨论】:

  • @chris85 我想也许有一个预建的功能。我想我会编写自己的函数来完成它。

标签: php explode


【解决方案1】:

这似乎很容易使用基本的字符串函数来完成。

$x = strrpos($string, ' and ');
$s2 = array(substr($string, 0, $x), substr($string, $x + 5));

【讨论】:

  • 嗯,是的。这不是你想要的吗?
  • 当然,但我的功能适用于每个单词,无论长度如何。看看吧。
  • 我只是想说明一个概念。我相信这个概念应该在函数中同样有效。
  • 5 =>  和 
【解决方案2】:

我的方法更简单了,而是使用preg_match_all() 和一个简单的正则表达式:

$string = "one and two and three and four and five";
$pattern = '/(\w+\s)+/';
preg_match_all($pattern, $string, $matches);

$length = strlen($matches[0][0]);

echo $matches[0][0]; // 'one and two and three and four and'
echo substr($string, $length); // 'five'

这里唯一的问题是第一个匹配仍然有一个尾随的“和”,如果需要,可以通过一些简单的编码来摆脱它。如果您想要更复杂的正则表达式,您可以使用正面展望和负面展望。

【讨论】:

  • 是的。看看我的功能。它没有尾随的“和”。
  • 酷 - 它比我在这里做的更冗长@frosty
  • 好吧,好在它是一个函数,所以这是我第一次也是最后一次不得不再次编写它。
  • 没错,我可以将它创建为一个函数。这就是代码的美妙之处:)
  • 就个人而言,只要我的代码中有任何重复至少一次的内容,我都会为它编写一个函数。这将代码至少减少了一半,并且使编码变得更加简单。我不得不说,函数可能是我在编码中最喜欢的。
【解决方案3】:

你也可以使用preg_split:

单字符分隔符

$string = "one,two,three,four,five";
$delimiter = ",";

// The regexp will look like this:  /,([^,]+)$/   
$array = preg_split("/".$delimiter."([^".$delimiter."]+)$/", $string,
  -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

print_r($array);

正则表达式将最后一个分隔符与最后一个项目匹配,但仅捕获该项目,以便通过 PREG_SPLIT_DELIM_CAPTURE 将其保留在结果中。

PREG_SPLIT_NO_EMPTY 在那里,因为我不知道为什么,拆分在最后给出了一个空字符串。

多字符分隔符

这里必须调整正则表达式,使用否定环视(如this answer 中所述):

$string = "one and two and three and four and five";
$delimiter = " and ";

$array = preg_split("/$delimiter((?(?!$delimiter).)*$)/", $string,
  -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

print_r($array);

(?(?!$delimiter).)* 表示:仅匹配不以单词$delimiter 开头的字符。第一个? 防止捕获其他组。

【讨论】:

  • 最后我想在这种情况下使用 preg_split 可能不是最好的主意......
【解决方案4】:

我不知道是否有任何其他快速方法可以解决该需求,但您可以尝试以下代码;

$string = "one and two and three and four and five";
$stringArray = explode(" and ", $string);
$stringArrayItemCount = count($stringArray);
//Keep your last item
$stringArrayLastItem = $stringArray[$stringArrayItemCount-1];
//Remove last item from your array
unset($stringArray[$stringArrayItemCount-1]);
//Create new array imploding existing one + last item of your old array
$stringArray = array(implode(" and ",$stringArray),$stringArrayLastItem);

print_r($stringArray);

此示例的工作版本:http://ideone.com/qdZFtk

希望这会有所帮助。

【讨论】:

    【解决方案5】:

    为此编写了一个函数:

    <?php
    
    $string = "one and two and three and four and five";
    $string = explodeLast(" and ", $string);
    
    echo $string;
    
    function explodeLast($explodeAt, $string) {
    
    $explode = explode($explodeAt, $string);
    $count = count($explode);
    $counter = 0;
    $string = null;
    
    while ($counter < $count-1) {
    
    if ($counter < $count-2) {
    $string .= $explode[$counter].$explodeAt;
    } //end of if ($counter < $count-2)
    else {
    $string .= $explode[$counter];
    } //end of else not ($counter < $count-2)
    
    $counter++;
    } //end of while ($counter < $count)
    
    return $string;
    
    } //end of function explodeLast($explode, $explodeAt)
    
    ?>
    

    【讨论】:

      【解决方案6】:

      这应该可行:

      $str = 'one and two and three and four and five';
      $breakWord = ' and ';
      
      $output = str_split($str, strrpos($str, $breakWord) + strlen($breakWord));
      
      var_dump($output);
      

      http://sandbox.onlinephpfunctions.com/code/0149b3ff973485befe97a1c6b241a6764bd2f289

      编辑 - 正则表达式使用:

      <?php
      $str = 'one and two and three and four and the last part is actually longer than the first part';
      $pattern = "/(.+) and (?!.* and )(.+)/";
      preg_match($pattern, $str, $matches);
      
      var_dump($matches);
      

      http://sandbox.onlinephpfunctions.com/code/0bc68f46fe594e360134bdca256e5916a2f42f74

      【讨论】:

      • 我也想过这个,但是在$str = 'one and two and three and four and the last part is actually longer than the first part';时遇到了问题
      • 这个怎么样? sandbox.onlinephpfunctions.com/code/…
      • 是的 - 我在发布这个帖子后也在这个帖子中看到了它......但这是“新”:) sandbox.onlinephpfunctions.com/code/…
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-17
      • 1970-01-01
      • 1970-01-01
      • 2020-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多