Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I write this code less and better in Laravel?

How can I write this code less and better in Laravel?
I am writing this code to remove the / r character in the input text and also to calculate the number of characters in the text.

$removeRchar = Str::remove("\r", $request['message']);
    $length = Str::length($removeRchar);
    if ($length <= 70) {
        $pages = 1;
    } else {
        $pages = ceil($length / 70);
    }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You could create a Stringable

$length = Str::of($request['message'])
    ->remove('\r')
    ->length();

$pages = ceil($length / 70);

If you don’t want an extra page when you have a lenght of 70, 140 etc. you should add a check. For example:

$pages = $lenght % 70 === 0 
    ? $lenght / 70
    : ceil($length / 70);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading