Categories
Javascript Laravel

Change DB Connection at Runtime in Laravel

\Config::set("database.connections.mysql", [
    "host" => "your-host.com",
    "driver" => "mysql",
    "database" => 'database-name',
    "username" => "username",
    "password" => "password",
]);
\DB::purge('mysql');
Categories
Laravel

PhpSpreadsheet Issue With Vertical Tabs (0x0B)

This article is about fixing the following error:

Failed to load path_to_project/storage/framework/laravel-excel/laravel-excel-eTdm3TlUf1neh8QbolfXmodtigO50x3x.html as a DOM Document

Recently I caught a bug where PHP excel parser (PhpSpreadsheet) was failing when parsing some rows fetched from the database. With a series of trial and error tests, I found the row that was causing the failure.

So, I copied the sentence in Sublime Text and saw that there was a hex character (0x0b) in the sentence. This character is known as a “Vertical Space” and is visually invisible in most editors. I couldn’t see this character in Mysql Workbench so I copied the whole row in Sublime text and saw the hex representation of this character. Below are some other representations of this character:

  • 0x0b
  • “\v”

To fix the issue, I ran the following SQL query to find all sentences containing this character and then removed them from the database:

SELECT text FROM messages WHERE text LIKE CONCAT('%',0x0b,'%');

It’s a good practice to filter your inputs and make sure you’re not storing unwanted characters like these in the database, they might bite you one day!

Categories
Javascript

4 Ways To Check If A Variable Is A Number In JS + Comparison

Comparison Table

Case/^\d+$/
.test(variable)
/^[0-9]+$/
.test(variable)
!isNaN
(variable)
typeof
variable
“123”TrueTrueTruestring
” “FalseFalseTruestring
123TrueTrueTruenumber
“1.23”FalseFalseTruestring
1.23FalseFalseTruenumber
“abc”FalseFalseFalsestring
Comparison of Different Methods to Find Out if a Variable Is a Number in JS