-1

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.

Anshul
  • 464
  • 4
  • 14
Petro Gromovo
  • 1,755
  • 5
  • 33
  • 91
  • I think you should generate file name with the timestamp and save it as the filename in the same record (create a new field file_name in the table). It will be unique and you can get corensponding filename. – Anshul Jun 12 '19 at 12:17
  • No, in my task I need to render item with title “Demo List File.csv” into file “Demo List File.csv”, or something very similar, but I just want to exclude invalid chars in title name and if nonenglish leeter would be used in title field. No timestamp and no additive fields. – Petro Gromovo Jun 12 '19 at 12:27

1 Answers1

2

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

Anshul
  • 464
  • 4
  • 14
  • Yes, that could be useful, but I need I have to know ALL invalid chars for ubuntu filename. That is complicated and with non english language to something impossible.Any other decision ? – Petro Gromovo Jun 12 '19 at 15:22
  • 1
    All [A-Za-z0-9] characters are safe for the file name. You can modify regular expression according to your need. For more details Kindly check this https://stackoverflow.com/questions/457994/what-characters-should-be-restricted-from-a-unix-file-name – Anshul Jun 13 '19 at 03:58