【问题标题】:Find and replace the last instance of a substring in a string using PHP [duplicate]使用PHP查找并替换字符串中子字符串的最后一个实例[重复]
【发布时间】:2018-02-22 16:26:37
【问题描述】:

我有一个动态的运动列表。每项运动都以, 结尾。最后一个, 使用$sports = substr($sports, 0, -2); 删除。现在我想弄清楚如何用, and(逗号和空格)替换最后一个,(逗号空格)。我可能错过了它,但我没有看到这个功能。是否有一种或另一种创造性的方式来实现它?

原始列表

Football, Soccer, Basketball, Swimming, Baseball, Golf, 

所需列表

Football, Soccer, Basketball, Swimming, Baseball, and Golf 

【问题讨论】:

标签: php


【解决方案1】:
$str = substr("Football, Soccer, Basketball, Swimming, Baseball, Golf, ", 0, -2);

$last_comma_position = strrpos($str, ',');

if($last_comma_position != false)
{
    echo substr_replace($str, ' and', $last_comma_position, 1);
}

http://php.net/substr_replace

或者作为一个函数

function replace_last($haystack, $needle, $replaceWith)
{
    $last_position = strrpos($haystack, $needle);

    if($last_position != false)
    {
        return substr_replace($haystack, $replaceWith, $last_position, strlen($needle));
    }
}

$str = substr("Football, Soccer, Basketball, Swimming, Baseball, Golf, ", 0, -2);

echo replace_last($str, ',', ' and');

都打印出来

Football, Soccer, Basketball, Swimming, Baseball and Golf

【讨论】:

    【解决方案2】:

    您可以利用explode()/implode() 来执行此操作。检查live example here

    $list = 'Football, Soccer, Basketball, Swimming, Baseball, Golf, ';
    
    function display( $string ) {
      // Splitting the list by comma
      $str = explode(',', $string);
    
      // Removing empty items
      $str = array_filter($str, function($item) {
        return strlen(trim($item)) > 0;
      });
    
      // prepeding "and" before the last item only if it contains more than one item
      $size = count($str);
    
      if( $size > 1 ) {
        $str[$size - 1] = "and " . (string) $str[$size - 1];
      }
    
      // Make list great (string) again
      $str = implode(', ', $str);  
    
      return $str;
    }
    
    
    echo display($list);
    

    会回声:

    足球、足球、篮球、游泳、棒球和高尔夫

    【讨论】:

      【解决方案3】:

      你可以试试你自己的方法。像这样的东西:

      $string = "Football, Soccer, Basketball, Swimming, Baseball, Golf,"; 
      
      function addAndToList($string) {
      
      $string = substr($string, 0, -1);
      $stringArray = explode(',', $string); 
      
      $resultString = ''; 
      foreach ($stringArray as $index => $val) {
        if (!isset($stringArray[$index+1])) 
             $resultString .= 'and '.$val; 
        else 
            $resultString .= $val.', ';
      }
      
      return $resultString; 
      }
      
      echo addAndToList($string); 
      

      【讨论】:

        【解决方案4】:

        如果你的变量已经定义,试试这个:

        $arr = array_reverse(explode(', ', $sports));
        $last = array_shift($arr);
        $arr[0] .= " and {$last}";
        $sports = implode(', ', array_reverse($arr));
        echo "$sports\n";
        

        【讨论】:

        • 感谢您提出我最初的解决方案:-)
        【解决方案5】:

        解释:

        • 匹配字符串的最后一个单词
        • 由空格字符分隔
        • 同时将其存储到反向引用伪变量$1
        • 添加所需的字符串
        #!/usr/bin/env php
        <?php
        
        $sports = "Football, Soccer, Basketball, Swimming, Baseball, Golf, ";
        $sports = substr($sports, 0, -2);
        
        echo preg_replace('/ ([a-zA-Z]+)$/', ' and $1', $sports) . "\n";
        

        这个 sn-ps 输出:

        Football, Soccer, Basketball, Swimming, Baseball, and Golf
        

        解决方案可以在这个demo验证。

        【讨论】:

          猜你喜欢
          • 2011-12-27
          • 2016-01-31
          • 1970-01-01
          • 1970-01-01
          • 2013-01-07
          • 1970-01-01
          • 1970-01-01
          • 2014-09-19
          • 2013-06-30
          相关资源
          最近更新 更多