代码之家  ›  专栏  ›  技术社区  ›  Mr. B

使用PHP[复制]查找并替换字符串中子字符串的最后一个实例

php
  •  -1
  • Mr. B  · 技术社区  · 6 年前

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

    原始列表

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

    所需列表

    Football, Soccer, Basketball, Swimming, Baseball, and Golf 
    
    5 回复  |  直到 6 年前
        1
  •  3
  •   ezw    6 年前
    $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
  •  1
  •   Anwar    6 年前

    你可以利用 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
  •  0
  •   bos570    6 年前

    你可以试试你自己的方法。像这样的事情:

    $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
  •  0
  •   Félix Adriyel Gagnon-Grenier    6 年前

    如果变量已定义,请尝试此操作:

    $arr = array_reverse(explode(', ', $sports));
    $last = array_shift($arr);
    $arr[0] .= " and {$last}";
    $sports = implode(', ', array_reverse($arr));
    echo "$sports\n";
    
        5
  •  0
  •   helvete rosaforrestiana    6 年前

    说明:

    • 匹配字符串的最后一个字
    • 由空格分隔
    • 将其存储到反向引用伪变量中 $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";
    

    此代码段输出:

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

    该解决方案可以在以下方面进行验证 demo .