【问题标题】:Split a string on the last occurring space在最后出现的空间上拆分字符串
【发布时间】:2012-04-11 02:49:07
【问题描述】:

我需要将一个字符串分成两部分。该字符串包含由空格分隔的单词,并且可以包含任意数量的单词,例如:

$string = "one two three four five";

第一部分需要包含除最后一个单词之外的所有单词。

第二部分只需要包含最后一个单词。

编辑:这两部分需要作为字符串返回,而不是数组,例如:

$part1 = "one two three four";

$part2 = "five";

【问题讨论】:

  • strrpos 将是一个很好的起点。手册有更多内容。
  • 这不是对确切问题的回答,而是与标题相关,因为我一直在寻找 几乎 相等的两个部分,而我通过使用 wordwrap 实现了这一点。

标签: php split explode


【解决方案1】:

贪婪地遍历输入字符串中的字符,然后匹配最近出现的空间并在其上进行拆分。超级简单,无需环顾或捕获或任何其他减慢正则表达式引擎的原因。

\K 的意思是“在此之前忘记所有匹配的字符”。

代码:(Demo)

$string = "one two three four five";
var_export(
    preg_split('/.*\K /', $string)
);

输出:

array (
  0 => 'one two three four',
  1 => 'five',
)

或作为单个变量:(Demo)

[$part1, $part2] = preg_split('/.*\K /', $string);
var_export($part1);
echo "\n";
var_export($part2);

【讨论】:

    【解决方案2】:

    有几种方法可以解决。

    数组操作:

    $string ="one two three four five";
    $words = explode(' ', $string);
    $last_word = array_pop($words);
    $first_chunk = implode(' ', $words);
    

    字符串操作:

    $string="one two three four five";
    $last_space = strrpos($string, ' ');
    $last_word = substr($string, $last_space);
    $first_chunk = substr($string, 0, $last_space);
    

    【讨论】:

    • 从逻辑上讲,由于 OP 使用字符串而不使用数组,我会说使用“非”数组选项,因为它们不需要数组(使代码看起来更合乎逻辑,因为它只是工作带字符串)但是有什么性能差异吗?
    【解决方案3】:

    我在 Perl 中的解决方案 :)... PHP 和 Perl 很相似 :) $string="一五三四五";

    @s = split(/\s+/, $string) ;
    
    $s1 = $string ;
    $s1 =~ s/$s[-1]$//e ;
    
    $s2 = $s[-1] ;
    print "The first part: $s1 \n";
    print "The second part: $s2 \n";
    

    【讨论】:

      【解决方案4】:
      $string="one two three four five";
      $matches = array();
      preg_match('/(.*?)(\w+)$/', $string, $matches);
      print_r($matches);
      

      输出:

      Array ( [0] => one two three four five [1] => one two three four [2] => five )

      那么您的零件将是 $matches[1]$matches[2]

      【讨论】:

        【解决方案5】:

        这样的东西可以做到,虽然它不是特别优雅。

        $string=explode(" ", $string);
        $new_string_1=$string[0]." ".$string[1]." ".$string[2]." ".$string[3];
        $new_string_2=$string[4];
        

        【讨论】:

          【解决方案6】:

          使用strrpos 获取最后一个空格字符的位置,然后使用substr 将字符串除以该位置。

          <?php
              $string = 'one two three four five';
              $pos = strrpos($string, ' ');
              $first = substr($string, 0, $pos);
              $second = substr($string, $pos + 1);
              var_dump($first, $second);
          ?>
          

          Live example

          【讨论】:

            【解决方案7】:

            应该这样做:

            $arr = explode(' ', $string);
            $second = array_pop($arr);
            $result[] = implode(' ', $arr);
            $result[] = $second;
            

            【讨论】:

              【解决方案8】:
              $string="one two three four five";
              
              list($second,$first) = explode(' ',strrev($string),2);
              $first = strrev($first);
              $second = strrev($second);
              
              var_dump($first);
              var_dump($second);
              

              【讨论】:

                【解决方案9】:
                $string = "one two three four five";
                $array = explode(" ", $string); // Split string into an array
                
                $lastWord = array_pop($array); // Get the last word
                // $array now contains the first four words
                

                【讨论】:

                  【解决方案10】:

                  看看 PHP 中的 explode 函数

                  返回一个字符串数组,每个字符串都是在字符串分隔符形成的边界上拆分字符串形成的子字符串

                  【讨论】:

                    【解决方案11】:

                    您需要在最后一个空格处拆分输入字符串。现在最后一个空格是后面没有任何空格的空格。所以你可以使用否定的前瞻断言来找到最后一个空格:

                    $string="one two three four five";
                    $pieces = preg_split('/ (?!.* )/',$string);
                    

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 2017-02-23
                      • 1970-01-01
                      • 1970-01-01
                      • 2018-12-06
                      • 2014-01-12
                      • 2014-01-21
                      • 2016-10-24
                      相关资源
                      最近更新 更多