Detect Language / Unicode Scripts in PHP

I've created packages that depending on knowing the text script/language to decide which font to use. For that I've created php-string-script-language which will do that exactly. You include it in your project and use it as below: use LasseRafn\StringScript; StringScript::isThai('Hello world.'); // false StringScript::isChinese('你好世界。…

User Initial Avatars

I recently published an article about generating initial avatars in PHP. In extension of that, I created a public and free API that does this for you. It has most of the settings, fast response and is open source. In the coming weeks, I will be tweaking and optimizing the…

Localized Dates In Laravel and Carbon

By adding one single line of code to any of your service providers in Laravel, your $user->created_at->diffForHumans() will be localized. Code to add: Carbon::setLocale(app()->getLocale()); Example: <?php namespace App\Providers; use Carbon\Carbon; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends…

PHP Ordrestyring v2 API Client Library

This one has been gathering dust in my private repositories for some time, so I decided to make it publicly available. It's a simple client library used to connect to Ordrestyring. I'm continously adding more supported endpoints, tests and methods as I find time or need them internally for our…

Advanced Route Model Binding in Laravel

So, imagine you have a platform on which each user has a list of Invoices, and those invoices have a number starting from 1000 for each user. You might still want to use URLs and endpoints like: /invoices/1005 instead of the primary key in the database. So, naively you…

Generating Initials from Names with PHP Initials Package

For a lot of projects, we use initials (Lasse Rafn = LR or LRA) to display employees and users. Repeating code over and over eventually felt wrong, so I packed the primary functionality into two different packages. Prepare yourself for some shameless self-promotion... okay, I do feel a bit shame. 1.…

Making Classes Iterable with ArrayAccess in PHP

I'm writing quite a lot API wrappers in PHP, and most GET index responses are paginated, so usually I return a PaginatedResponse class to the user of the wrapper, that has some different properties such as: items, page, totalPages and so on. However, for responses that are not paginated, I…

Faster Tests Using Stub Database Files

While building a larger application that has on a lot of feature tests which depends on data from the database, I found myself waiting upwards 10 minutes for my test-suite to finish, mostly due to migrations. Surely this is not an issue, and I've seen worse, but what if I…

Faster Array Merging in PHP

I recently read a tweet (that I'm unable to find) stating that adding two arrays together is faster than using array_merge, so I decided to test it. I wrote a quick file that contains 3 for loops, each doing 10.000.000 iterations. Below are the benchmarks. Benchmarks Below…

Using Migrations to Modify Data in Production

The initial work on a project is always easier when it comes to the database and data therein. Things change a bit after going live, where a migration could potentially cause loss of data, or a migration would also require some data to be moved elsewhere or even modified. I've…