0

Let's say that

$result='first';

when I do:

$result.='second';

I got 'first second' instead of 'firstsecond'. How to perfom concatenatation without adding this whitespace?

Edit:

That's the code. It should translate simple string to "pilots alphabet".

function converting($words){
  echo $words;
  global $result;
  $result='';
  for($x=0; $x<strlen($words); $x++){
    switch($words{$x}){
      case ' ':
        $result.=' ';
        break;
      case 'A':
        $result.='Alfa';
        break;
      case 'B':
        $result.='Bravo';
        break;
      case 'C':
        $result.='Charlie';
        break;
      case 'D':
        $result.='Delta';
        break;
      case 'E':
        $result.='Echo';
        break;
      case 'F':
        $result.='Foxtrot';
        break;
      case 'G':
        $result.='Golf';
        break;
      case 'H':
        $result.='Hotel';
        break;
      case 'I':
        $result.='India';
        break;
      case 'J':
        $result.='Juliett';
        break;
      case 'K':
        $result.='Kilo';
        break;
      case 'L':
        $result.='Lima';
        break;
      case 'M':
        $result.='Mike';
        break;
      case 'N':
        $result.='November';
        break;
      case 'O':
        $result.='Oscar';
        break;
      case 'P':
        $result.='Papa';
        break;
      case 'Q':
        $result.='Quebec';
        break;
      case 'R':
        $result.='Romeo';
        break;
      case 'S':
        $result.='Sierra';
        break;
      case 'T':
        $result.='Tango';
        break;
      case 'U':
        $result.='Uniform';
        break;
      case 'V':
        $result.='Victor';
        break;
      case 'W':
        $result.='Whiskey';
        break;
      case 'X':
        $result.='Xray';
        break;
      case 'Y':
        $result.='Yankee';
        break;
      case 'Z':
        $result.='Zulu';
        break;
    }
  }
  return $result;
}

That switch also adds some cases that I have not defined. It is adding other characters to result, like '?' or '!' if they appear in 'words' string.

14
  • 5
    The .= operator does not add space. The space is already in one of your strings. Commented Oct 4, 2017 at 19:40
  • 1
    as above, but if you need to you could use trim() on the string before concatenation Commented Oct 4, 2017 at 19:42
  • 1
    "Well I am sure it's not." I'll bet you a beer it is. :) Commented Oct 4, 2017 at 19:43
  • 1
    It seems to work correctly: ideone.com/STKwb7 Commented Oct 4, 2017 at 19:54
  • 1
    The only space in the output is from the space in the input string, which is because of case ' ': Commented Oct 4, 2017 at 19:55

2 Answers 2

3

While I can't identify your problem (other posters are correct, your code is essentially right, the problem must be in the souce string), you may want to consider this somewhat more simple solution:

<?php

function phoneticAlphabetTranslation($word) {
    $translations = [
        ' ' => ' ',
        'a' => 'Alpha',
        'b' => 'Beta',
        'c' => 'Charlie',
    ];

    return str_ireplace(array_keys($translations), array_values($translations), $word);
}

echo phoneticAlphabetTranslation('abAC');

each of the keys in $translations will be mapped to its value in the supplied string. Added benefit of being case-insensitive. If you don't desire case-insensitivity, just replace str_ireplace with str_replace.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! I am learning php since 2 days so that will be very helpful.
2

You may already have invisible whitespace in your strings.

Try passing the strings through trim before adding to the $result variable

$result = trim('first');
$result .= trim('second');
echo $result;

Alternatively remove all whitespace after joining.

echo str_replace(" ", "", $result)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.