Catch all names and put commas to separate them

13

When I get the names and add to a variable, I can not detach them with commas normally.

  • It must be by comma and dot at the end
  • I can not put each name in line break using

It's hard to explain, but here's an example:

while ($listintegrantes = mysqli_fetch_array($query1)) {
        $integrantes = "$integrantes" . $listintegrantes['nome'] . ",";
}

That is, this will show like this:

  

john, Cyrax, john2,

But I want you to stay:

  

john, Cyrax and john2.

How can this be done?

-SOLUTION

Here is the resolution of the problem posted above, all resolutions are correct, I have adapted to my problem so that my correct goal returns to me.

        $query1 = mysqli_query($conexao, $sql1);
        $integrantes = "";
        $i = mysqli_num_rows($query1);
        $x = 1;
        while ($listintegrantes = mysqli_fetch_array($query1)) {
            $integrantes = $integrantes . $listintegrantes['nome'];
            if ($x < $i - 1) {//antes do penultimo
                $integrantes = $integrantes . ", ";
            }elseif($x == $i-1){//penultimo
                $integrantes = $integrantes . " e ";
            }elseif($x == $i){//ultimo
                $integrantes = $integrantes . ".";
            }
            $x++;
        }

below is the feedback from the above algorithm: Cyrax, John, Serana and Smigol.

When there is only one member record: Serana.

    
asked by anonymous 27.04.2015 / 16:14

5 answers

9

You can do this:

$sinal = ", ";
$size = mysqli_num_rows($query1);
$i = 1;
while ($listintegrantes = mysqli_fetch_array($query1)) 
{
    if($i == $size-1) 
        $sinal = " e ";
    elseif($i == $size) 
        $sinal = ".";

    $integrantes .= $listintegrantes['nome'] . $sinal;
    $i++;
}
    
27.04.2015 / 16:25
9

Another way would be to simply remove the last element from the array, use implode to put the commas if there is enough name for the separation and then put the last element back.

    function names( array $names )
    {
        $_names = array_pop( $names );

        if( count( $names ) > 0 )
        return implode( ', ' , $names ) . ' e ' . $_names;

        return 'só ' . $_names;
    }

    // output: só eu
    echo names( ['eu'] );

    // output: eu e tu
    echo names( ['eu' , 'tu' ] );

    // output: eu, tu e ele
    echo names( ['eu' , 'tu' , 'ele' ] );
    
27.04.2015 / 16:42
7

Another way to do this is to use the opposite logic, concatenate the string with e (yes it has spaces) and in the end use preg_replace to replace all e with , except the last, this control is done with the fourth parameter that is the number of substitutions.

In the example of an array with five elements will become a string with four e , now just know the number of replacements made that is the total of elements less two that is 3.

Example 1

$itens = 1;
while ($listintegrantes = mysqli_fetch_array($query1)) {
       $integrantes = "$integrantes" . $listintegrantes['nome'] . " e ";
       $itens++;
}

$str = preg_replace('/ e /', ', ', $integrantes, $itens - 2);

Example 2 ideon

$arr = array('john', 'Cyrax', 'john', 'Sonia', 'Sector');
$str = implode(' e ', $arr);
$itens = count($arr);
$str_formatada = preg_replace('/ e /', ', ', $str, count($arr)-2);
echo $str_formatada;

saida : john, Cyrax, john, Sonia e Sector
    
27.04.2015 / 17:49
4

It could look like this:

while ($listintegrantes = mysqli_fetch_array($query1)) {
    $integrantes = "$integrantes" . $listintegrantes['nome'] . ",";
}

$integrantes = rtrim($integrantes, ',') . '.';
    
27.04.2015 / 16:27
4

Another possible solution is to get the position of the last occurrence of the character in the string strrpos

27.04.2015 / 18:33