5

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, $three_bed, $four_bed);

For this example lets say:

$studio = '1';
$one_bed = '3';
$two_bed = '3';

I then use the implode function to put a comma in between all the values:

$bedroom_list = implode(", ", array_filter($bedroom_array));
echo $bedroom_list;

This then outputs:

1, 2, 3

What I want to do is find the last comma in the string and replace it with an &, so it would read:

1, 2 & 3

The string will not always be this long, it can be shorter or longer, e.g. 1, 2, 3, 4 and so on. I have looked into using substr but am not sure if this will work for what I need?

iain
  • 407
  • 3
  • 20
  • I answered the same question here: [How to edit the implode so it will join values with two strings?](http://stackoverflow.com/questions/7613847/how-to-edit-the-implode-so-it-will-join-values-with-two-strings/7613905#7613905) – GolezTrol Oct 07 '11 at 13:06
  • possible duplicate of [Implode array with ", " and add "and " before last item](http://stackoverflow.com/questions/8586141/implode-array-with-and-add-and-before-last-item) – Angry Dan May 08 '15 at 14:23

8 Answers8

31

Pop off the last element, implode the rest together then stick the last one back on.

$bedroom_array = array('studio', 'one_bed', 'two_bed', 'three_bed', 'four_bed');
$last = array_pop($bedroom_array);
$string = count($bedroom_array) ? implode(", ", $bedroom_array) . " & " . $last : $last;

Convert & to the entity & if necessary.

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • 2
    I'd change `$string = implode(", ", $bedroom_array) . " & " . $last;` to `$string = count($bedroom_array) ? implode(", ", $bedroom_array) . " & " . $last : $last;` to cover cases where there was only one element in the array before the pop. Also, if this is just going straight to browser, use `' & '` instead of `' & '`. – daiscog Oct 07 '11 at 13:11
  • 1
    Here's a function version with a little error checking: http://www.ideone.com/SQZfi – Brad Christie Oct 07 '11 at 13:14
  • Thanks for your help! I tried using Micheals idea, but had some trouble as the variables in the array are not always set - hence the use of array_filter. I got the & working but it would still display the last array to I would get 1, 2 & 2 instead of 1 & 2. In the end I used Brads code which worked like a dream! I'd still be interested in other ways to do this though! – iain Oct 07 '11 at 15:02
6

if you have comma separated list of words you may use:

$keyword = "hello, sadasd, sdfgdsfg,sadfsdafsfd, ssdf, sdgdfg";
$keyword = preg_replace('/,([^,]*)$/', ' & \1', $keyword);
echo $keyword;

it will output: hello, sadasd, sdfgdsfg,sadfsdafsfd, ssdf & sdgdfg

codefreak
  • 6,950
  • 3
  • 42
  • 51
2

A one-liner alternative, that will work for any size array ($b = $bedroom_array):

echo count($b) <= 1 ? reset($b) : join(', ', array_slice($b, 0, -1)) . " & " . end($b); 
JRL
  • 76,767
  • 18
  • 98
  • 146
1
function fancy_implode($arr){
    array_push($arr, implode(' and ', array_splice($arr, -2)));
    return implode(', ', $arr);
}

I find this easier to read/understand and use

  • Does not modify the original array
  • Does not use regular expressions as those may fail if strings in the array contain commas, there could be a valid reason for that, something like this: array('Shirts (S, M, L)', 'Pants (72 x 37, 72 x 39)');
  • Delimiters don't have to be of the same length as with some of the other solutions
1

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

$index = strrpos( $str, ',' ); 
if( $index !== FALSE )
    $str[ $index ] = '&'; 
aziz punjani
  • 25,586
  • 9
  • 47
  • 56
0

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
$bedroom_list = implode(", ", array_filter($bedroom_array));

$vars =  $bedroom_list;

$last = strrchr($vars,",");

$last_ = str_replace(",","&",$last);

echo str_replace("$last","$last_",$vars);
Max
  • 15,693
  • 14
  • 81
  • 131
SamarLover
  • 182
  • 10
0
<?php
$string = "3, 4, 5";
echo $string = preg_replace('/,( \d)$/', ' &\1', $string);
?>
pb149
  • 2,298
  • 1
  • 22
  • 30