Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

laravel - How to remove certain characters in a string in blade template

I would like to know how to remove certain characters in a string inside blade templade, say:

the string is my string_345432.pdf and you just want to remove the number part _345432 and left with my string.pdf

question from:https://stackoverflow.com/questions/65933172/how-to-remove-certain-characters-in-a-string-in-blade-template

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I suppose you should do it in controller, not in template.

You can use regex for that smth like this:

preg_replace('/[a-zA-Z.]/', '', $string);

If you're really need it to use in blade template, you can create some additional file with helper function, and use it anywhere you whant:

function filterFileName($string) {
    return preg_replace('/[a-zA-Z.]/', '', $string);
}
  1. Create helpers.php file
  2. Add your function filterFileName in it.
  3. Add that file in composer.json and load it with composer dump-autoload
  4. Now, you can use it in template:
{{ filterFileName($string) }}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...