本文概述

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

在本文中, 尽管它看起来没用(可能确实是因为你很可能永远不需要它), 但是你将在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);

}

}

编码愉快!

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

  1. 如何在Tensorflow.js中处理MNIST图像数据

    by Kevin Scott 凯文·斯科特(Kevin Scott) 如何在Tensorflow.js中处理MNIST图像数据 (How to deal with MNIST image data i ...

  2. 如何在SpringBootOAuth服务器中实现双因素认证?第二部分:Under the Hood

    如何在SpringBootOAuth服务器中实现双因素认证?2:Under the Hood 原创2022-03-09 09:40·超级晴天ii 本文继续第1部分,演示SpringBootOAuth身 ...

  3. 如何在3ds max中创建可用于真人场景的巨型机器人:第 1部分

    推荐: NSDT场景编辑器助你快速搭建可二次开发的3D应用场景 1. 创建主体 步骤 1 打开 3ds Max. 打开 3ds Max 步骤 2 在左侧视口中,按键盘上的 Alt-B 键.它 打开视口 ...

  4. 如何在sqlite3连接中创建并调用自定义函数

    #!/user/bin/env python # @Time :2018/6/8 14:44 # @Author :PGIDYSQ #@File :CreateFunTest.py '''如何在sql ...

  5. Iar环境c语言调用汇编函数,如何在IAR EWARM中通过内联汇编程序在另一个模块中调用C函数?...

    我在硬故障处理程序中有一些程序集.程序集基本上是为了传递当前堆栈指针作为参数(在R0中).它看起来像这样...如何在IAR EWARM中通过内联汇编程序在另一个模块中调用C函数? __asm(&quo ...

  6. 学习如何在AutoCad土木工程中绘制建筑设计图

    学习如何在AutoCad中绘制建筑设计图从平面图到AutoCad土木工程中的整栋建筑 你会学到: 如何绘制房屋地图 如何绘制建筑设计 如何从AutoCad打印或出图 AutoCaD使用 AutoCaD ...

  7. 如何在OS X中打印到PDF文件

    如何在OS X中打印文件到PDF文件? 其实不需要安装任何其他软件,OS X本身支持打印到PDF文件这个功能. 具体操作详见下面文章: Want to save a document or web p ...

  8. 转 如何在IOS设备中去掉屏幕上的status bar

    引入 如何在IOS设备中去掉屏幕上的status bar,即:不显示设备上方的[网络.时间.电池??]条? 操作 方法一: 在-info.list项目文件中,加上"Status bar is ...

  9. react中纯函数_如何在纯React中创建电子邮件芯片

    react中纯函数 by Andreas Remdt 由Andreas Remdt 如何在纯React中创建电子邮件芯片 (How to create email chips in pure Reac ...

最新文章

  1. PL/SQL第五章 Order by排序
  2. 英国JIC院士3.8万英镑招博后-植物代谢物与微生物组-截止6月27日
  3. JS 之 innerHTML
  4. mysql用户权限设置
  5. python中文件分类_Python中的类是否在不同的文件中?
  6. 并发队列-无界非阻塞队列 ConcurrentLinkedQueue 原理探究
  7. b350主板支持cpu列表_惊!AMD旧主板通过这个方法竟也能支持PCIe4.0
  8. 虚机和实体服务器性能,虚机的性能主要与以下几方面有关
  9. 巩膜(眼白)灰色原因
  10. openMVG跑自定义数据出错
  11. 5月17日 AJAX 之 XML
  12. matlab支持向量机程序代做
  13. 深入浅出排序学习:写给程序员的算法系统开发实践
  14. java 取栈顶元素_java集合系列(7)Stack
  15. python入门系列:迭代器和生成器
  16. SpringBoot 2.1.0 整合 WebSocket 通信
  17. 实验5 数独游戏界面设计
  18. Atom : 一些有意思的插件
  19. React Native 布局实现测试
  20. php rm-rf,rm-rf误操作的恢复过程

热门文章

  1. 2017物流数据报告
  2. 关于获取微信小程序码的“47001”错误码的坑
  3. Java IO流--数据读写(字符/字节流/二进制文件)
  4. python 爬取豆瓣某一主题书单_Python爬虫 || 使用requests和xpath爬取豆瓣Top250书单内容全解。...
  5. 折线图 和如何在图上写字
  6. 家电 计算机和电讯领域 英语,美国电子电器工程硕士11个分支方向,你懂吗?...
  7. 独立站斗篷技术是什么
  8. 一边学计算机一边上班累的说说,对工作很累的句子说说心情
  9. 套接字、UDP通信、TCP通信、TCP/IP协议簇
  10. 喜马拉雅三战IPO: “声“意难做、4年累亏近30亿