Laravel 9 - The squish() helper method
Hello Artisans, today I'll talk about one of the handy method called squish() helper. Most of the times we need to remove the whitespaces from the user inputted text.
Note: Tested on Laravel 9.19.
squish() helper method
Of course we can use the PHP's by default method called trim(). But it'll only remove the whitespace from the beginning and ending of a string like below.
<?php $text = "\t\tHello world "; $trimmed = trim($text); // it'll print "Hello world"
So, we can easily remove the whitespace around the string. But may be there is an scenario where we've to remove the whitespace between the string. And where we need this squish() helper method which is introduced in Laravel 9 which is not only remove the whitespace from from the beginning and ending of a string it can also remove the whitespace between the string. Check the below code snippet.
<?php $text = ' Shouts.dev is awesome '; $squished = Str::squish($text); // which will print "Shouts.dev is awesome"
This also works in a nicer way like below
<?php $input = ' Hello world! '; $output = Str::of($input) ->replace('world', 'universe') ->squish(); // output: Hello universe!
So, we can replace the string and also trim the whitespace at once.
That's it for today. I hope you've enjoyed this tutorial. Thanks for reading. 🙂
Preview may take a few seconds to load.
Below you will find some common used markdown syntax. For a deeper dive in Markdown check out this Cheat Sheet
Bold & Italic
Bold **double asterisks**
Three back ticks and then enter your code blocks here.
# This is a Heading 1
## This is a Heading 2
### This is a Heading 3
> type a greater than sign and start typing your quote.
You can add links by adding text inside of  and the link inside of (), like so:
To add a numbered list you can simply start with a number and a ., like so:
1. The first item in my list
For an unordered list, you can add a dash -, like so:
- The start of my list
You can add images by selecting the image icon, which will upload and add an image to the editor, or you can manually add the image by adding an exclamation !, followed by the alt text inside of , and the image URL inside of (), like so:
To add a divider you can add three dashes or three asterisks:
--- or ***