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
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
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?
弦不会总是这么长,它可以更短或更长,例如1、2、3、4等等。我已经考虑过使用substr,但不确定它是否可以满足我的需要?
7 个解决方案
#1
25
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.
转换和到实体&放大器;如果有必要的话)。
#2
5
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;
$关键字= "hello, sadasd, sdfgdsfg,sadfsdafsfd, ssdf, sdgdfg";关键词=美元preg_replace(' /((^,)*)$ / ',' & \ 1 ',美元关键字);echo $关键词;
it will output: hello, sadasd, sdfgdsfg,sadfsdafsfd, ssdf & sdgdfg
它将输出:hello, sadasd, sdfgdsfg,sadfsdafsfd, ssdf和sdgdfg
#3
1
A one-liner alternative, that will work for any size array ($b = $bedroom_array):
一个简单的选择,适用于任何大小的数组($b = $bedroom_array):
echo count($b) <= 1 ? reset($b) : join(', ', array_slice($b, 0, -1)) . " & " . end($b);
#4
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)');
- 不使用正则表达式,因为如果数组中的字符串包含逗号,可能会失败,有一个合理的理由,比如:数组('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
- 分隔符不必与其他一些解决方案的长度相同
#5
0
$bedroom_list = implode(", ", array_filter($bedroom_array));
$vars = $bedroom_list;
$last = strrchr($vars,",");
$last_ = str_replace(",","&",$last);
echo str_replace("$last","$last_",$vars);
#6
0
<?php
$string = "3, 4, 5";
echo $string = preg_replace('/,( \d)$/', ' &\1', $string);
?>
#7
0
strrpos finds the last occurrance of a specified string. $str = '1, 2, 3';
strrpos发现指定字符串的最后一个发生。$str = ' 1,2,3 ';
$index = strrpos( $str, ',' );
if( $index !== FALSE )
$str[ $index ] = '&';
#1
25
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.
转换和到实体&放大器;如果有必要的话)。
#2
5
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;
$关键字= "hello, sadasd, sdfgdsfg,sadfsdafsfd, ssdf, sdgdfg";关键词=美元preg_replace(' /((^,)*)$ / ',' & \ 1 ',美元关键字);echo $关键词;
it will output: hello, sadasd, sdfgdsfg,sadfsdafsfd, ssdf & sdgdfg
它将输出:hello, sadasd, sdfgdsfg,sadfsdafsfd, ssdf和sdgdfg
#3
1
A one-liner alternative, that will work for any size array ($b = $bedroom_array):
一个简单的选择,适用于任何大小的数组($b = $bedroom_array):
echo count($b) <= 1 ? reset($b) : join(', ', array_slice($b, 0, -1)) . " & " . end($b);
#4
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)');
- 不使用正则表达式,因为如果数组中的字符串包含逗号,可能会失败,有一个合理的理由,比如:数组('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
- 分隔符不必与其他一些解决方案的长度相同
#5
0
$bedroom_list = implode(", ", array_filter($bedroom_array));
$vars = $bedroom_list;
$last = strrchr($vars,",");
$last_ = str_replace(",","&",$last);
echo str_replace("$last","$last_",$vars);
#6
0
<?php
$string = "3, 4, 5";
echo $string = preg_replace('/,( \d)$/', ' &\1', $string);
?>
#7
0
strrpos finds the last occurrance of a specified string. $str = '1, 2, 3';
strrpos发现指定字符串的最后一个发生。$str = ' 1,2,3 ';
$index = strrpos( $str, ',' );
if( $index !== FALSE )
$str[ $index ] = '&';