php+摩尔斯电码,如何在Symfony 3中使用PHP编码和解码摩尔斯电码(翻译摩尔斯电码)...

本文概述

莫尔斯电码是一种通过键入一系列电子脉冲(短脉冲(称为”点”)和长脉冲(” _”)表示)来发送文本消息的方法。尽管你可能认为此代码仅在电影中使用, 但是摩尔斯电码在业余无线电爱好者中仍然很流行, 这意味着它仍在使用。

在本文中, 尽管它看起来没用(可能确实是因为你很可能永远不需要它), 但是你将在Symfony 3项目中学习如何使用PHP将文本转换为莫尔斯电码, 反之亦然。

1.安装摩尔斯电码库

摩尔斯电码库是一个有用的类, 可让你轻松地将文本转换为摩尔斯电码和将摩尔斯电码转换为文本。你可以使用composer打开终端, 将其安装到Symfony项目中, 然后切换到项目目录, 然后运行以下命令:

composer require rexxars/morse

另外, 你可以手动修改composer.json文件, 并将该库添加为依赖项:

{

"require": {

"rexxars/morse": "^1.0"

}

}

然后运行composer install。安装后, 你将可以使用控制器中的库。该库由@rexxars编写, 有关更多信息, 请访问Github上的官方存储库。

2.在控制器中使用库

使用此库, 你可以将文本编码为摩尔斯电码表示形式, 并根据需要创建音频文件, 并将摩尔斯电码解码为文本。

编码(文本到莫尔斯电码)

要将文本编码为摩尔斯电码表示形式, 请创建一个摩尔斯电码实例, 然后使用toMorse方法。此方法将要转换为摩尔斯文本的文本作为第一个参数:

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);

}

}

你甚至可以从控制器返回WAV(音频)文件作为响应:

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')

);

}

}

解码(莫尔斯码)

要解码摩尔斯电码并获取其文本表示, 请创建摩尔斯电码的实例, 然后使用fromMorse方法。此方法将要转换为摩尔斯文本的文本作为第一个参数:

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);

}

}

编码愉快!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值