0

I use this code to display first letters from a string where $atit is an array of that string and results must be something like "R. & B. & T.". But this code displays results like "R. & B. & T. &".

Is it possible to get rid of the last "&"? I tried to use rtrim in echo line but it deleted all "&" while I need to delete only the last one.

foreach ($atit as $ati) {
   $pieces = explode(' ', $ati);
   $otw = preg_replace('/\W\w+\s*(\W*)$/', '$1', $pieces);  
   $acronym = "";

   foreach ($otw as $w) {
      $acronym .= $w[0].'. ';
   }

   $bla = $acronym.' & ';

   echo $bla;
} 

I think it's a problem with foreach but I couldn't figure it out. Any suggestions please?

Update:

Each entry in the array is comprised of several words so the code above is designed to display first letters of all words except for the last one in each entry.

Example:

$atit = ["Right now", "Behind you", "This is the time"];

So the code above will first delete the last word in each array entry and then get and display the first letters of the rest words in it, something like this (R. & B. & T. I. T). but the problem is that it adds an additional "&" to the end (R. & B. & T. I. T &).

luchaninov
  • 6,792
  • 6
  • 60
  • 75
nisr
  • 75
  • 1
  • 9

3 Answers3

1

If you meant formation of an "acronym" comprising of first character of each string from the array of strings - here is complex solution using array_map, array_slice, implode, explode and ucfirst functions:

$atit = ["Right now", "Behind you", "This is the time"];

$acronym = implode(" & ", array_map(function($v) {
    $words = explode(" ", $v);
    if (count($words) > 2) {
        return implode(".", array_map(function($w) {
                    return ucfirst($w[0]);
                }, array_slice($words, 0, -1)));
    }
    return $words[0][0] . ".";  // for simple strings (less/equal two words)
}, $atit));

echo $acronym;  // "R. & B. & T.I.T"
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • thanks a lot but I need the rest of code. you can see why in the question update. waiting for your response – nisr May 11 '16 at 20:34
1

When doing a concatenation of multiple elements with separators like this, I prefer to check the result variable each time. If NOT empty, I prepend the concatenation separator. If empty, I don't. That way, you don't end up with the unneeded separator at the end.

(Or just create an array of elements and use the PHP implode() function)

For example:

$array = array("a", "b", "c", "d");
$result = "";
foreach ($array as $a)
{
   if ($result != "") { $result .= " & "; }
   $result .= $a;
}
// $result = "a & b & c & d"
Ryan Griggs
  • 2,457
  • 2
  • 35
  • 58
1

I don't understand why do you need that regex and why are you printing separate string pieces in every loop iteration.

I can suggest this solution:

$atit = ["Right now", "Behind you", "This is the time"];

$partList = array();
foreach ($atit as $ati) {
    $pieces = explode(' ', $ati);
    $numberOfPieces = sizeof($pieces);
    if ($numberOfPieces > 1) {
        $pieces = array_slice($pieces, 0, $numberOfPieces-1);
    }

    $acronym = "";
    foreach ($pieces as $w) {
        $acronym .= strtoupper($w[0]).'. ';
    }

    $partList[] = $acronym;
}

$bla = implode('& ', $partList);
echo $bla;
ArturOlszak
  • 2,653
  • 2
  • 21
  • 20