Learn how to conver text to morse code and viceversa with PHP in your Symfony 3 project.

Morse code is a method of sending text messages by keying in a series of electronic pulses, represented by a short pulse (called a "dot" .) and a long pulse (a "dash" _). Although you may think that this code is only used in films, Morse code is still popular among amateur radio enthusiasts, that means that it's still being used.

In this article, although it may look useless(and probably it is indeed because you are most likely to never need this), you will learn how to convert text to morse code and viceversa with PHP in your Symfony 3 project.

1. Install the Morse Code Library

The Morse Code library is an useful class that allows you to convert text to Morse Code and Morse Code to text easily. You can install it in your Symfony project with composer opening a terminal, switching to the directory of your project and then running the following command:

composer require rexxars/morse

Alternatively, you can modify manually your composer.json file and adding the library as a dependency:

{
    "require": {
        "rexxars/morse": "^1.0"
    }
}

Then run composer install. After the installation you'll be able to use the library from your controllers. This library has been written by @rexxars, for more information visit the official repository in Github here.

2. Using the library in your controllers

With this library you'll be able to encode text to its morse code representation and create an Audio file if you want, and decode morse code into text.

Encode (text to morse)

To encode text into its morse code representation, create an instance of the Morsecode and use the toMorse method for it. This method expects the text that you want to convert into morse as first argument:

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * Include the Morse Code library
 */
use Morse\Text as MorseText;

class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction(Request $request)
    {
        // Create an instance of Morse Code
        $text = new MorseText();

        // The text that you want to convert to morse
        $originalText = "Hello, this is the text that I want to encode";

        // Encode the text using the encode method of the morse instance
        $morseResult = $text->toMorse($originalText);

        // Return the morse code as response from your controller
        // .... . .-.. .-.. --- --..-- - .... .. ... .. ... - .... . - . -..- - - .... .- - .. .-- .- -. - - --- . -. -.-. --- -.. .
        return new Response($morseResult);
    }
}

You can even return a WAV (audio) file as response from your controller:

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * Include the Morse Code library
 */
use Morse\Wav as MorseWav;

class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction(Request $request)
    {
        // Create an instance of Morse Code
        $wav = new MorseWav();

        // The text that you want to convert to morse
        $originalText = "Hello World";

        // Encode the text using the encode method of the morse instance
        $morseWAVBuffer = $wav->generate($originalText);

        // Return the morse code as response from your controller
        return new Response(
            // Set content of the response the WAV Buffer result
            $morseWAVBuffer,
            // Set OK status code
            Response::HTTP_OK,
            // Send the response as a WAV file
            array('content-type' => 'audio/wav')   
        );
    }
}

Decode (morse to code)

To decode morse code and get its text representation, create an instance of the Morsecode and use the fromMorse method for it. This method expects the text that you want to convert into morse as first argument:

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * Include the Morse Code library
 */
use Morse\Text as MorseText;

class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction(Request $request)
    {
        // Create an instance of Morse Code
        $text = new MorseText();

        // The morse code that you want to convert into text
        $originalMorse = ".... . .-.. .-.. --- --..-- - .... .. ... .. ... - .... . - . -..- - - .... .- - .. .-- .- -. - - --- . -. -.-. --- -.. .";

        // Decode the morse code using fromMorse
        $textResult = $text->fromMorse($originalMorse);
        
        // Return the decoded text as response
        // HELLO,THISISTHETEXTTHATIWANTTOENCODE
        return new Response($textResult);
    }
}

Happy coding !


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors