laravel-5 - Exclude special characters from title to use it as a file name?
问题描述
In laravel 5.8 I need to write some content from DB to files on disks where the name of the file would be title field and I wonder how can I remove special characters from the title?
The title field is text English commonly but can contain digits or maybe different chars like “&^<”.
Does laravel/PHP have any method to remove these special characters?
Ubuntu 18, LAMP, php 7.2 used.
解决方案
You can use regular expression for this task. There is a method preg_replace in PHP which works with regular expression.
$title = preg_replace("/[^A-Za-z0-9 ]/", '', $title);
It will replace all non-alphanumeric characters from your title.
If you want to exclude numeric character then you can modify it to
$title = preg_replace("/[^A-Za-z ]/", '', $title);
Hope it will help :)
推荐阅读
- c++ - 类没有名为的成员
- gulp - gulp-template (lodash) 被 javascript 模板所覆盖
- azure - QnA Maker 服务地点仅限于美国西部
- facebook-graph-api - 如何在 Facebook Graph API 中按名称搜索页面,使用 Python 调用最少?
- swift - Swift 4 - 如何在 typealias 的完成处理程序帮助下获得结果(字典和数据)
- php - PHP/Laravel:使用特定键名创建 JSON
- rxjs - forkJoin 不触发响应
- r - 将值增量保存到 R 中的数据框
- jetty - 如何在 Eclipse-jetty-websocket version-8 中将授权标头添加到客户端握手请求
- python - 如何获取标签下的文字