Replace the last comma with an & sign

后端 未结 8 1337
小鲜肉
小鲜肉 2020-12-09 23:21

I have searched everywhere but can\'t find a solution that works for me.

I have the following:

$bedroom_array = array($studio, $one_bed, $two_bed, $t         


        
相关标签:
8条回答
  • 2020-12-10 00:11

    strrpos finds the last occurrance of a specified string. $str = '1, 2, 3';

    $index = strrpos( $str, ',' ); 
    if( $index !== FALSE )
        $str[ $index ] = '&'; 
    
    0 讨论(0)
  • 2020-12-10 00:12

    Here's another way to do the replacement with a regular expression using a positive lookahead which doesn't require a backreference:

    $bedroom_list = preg_replace('/,(?=[^,]*$)/',' &', implode(', ', $bedroom_array));
    
    0 讨论(0)
提交回复
热议问题