How to access language var from PHP in Grav CMS?

Gravs documentation is a little bit harsh on PHP developers that want to write plugins. What can you do in this situation? Look trough source code, check other plugins or var_dump $this object 🙂

I’ve spent some time searching for an answer how to access language var in plugins class. And this is it:

// Will output two letter ISO 3166-1 alpha-2 code of country
$language =  $this->grav['language']; 
$language->getLanguage();

Have fun with coding and don’t get crazy during that 🙂

Brutal fix for “Incorrect string value:” error in Matomo (Piwik)

If you’re using Matomo (previously Piwik) on MS IIS and MariaDB  and you’re getting a lot of these:

[06-Jun-2018 11:56:36 UTC] Error in Matomo (tracker): Error query: SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect string value: '\xC3_\xC6\x92\xC3\x82...' for column 'name' at row 1 In query: INSERT INTO matomo_log_action (name, hash, type, url_prefix) VALUES (?,CRC32(?),?,?) Parameters: array (   0 => 'website.com/Ă_ƒÂ¢',   1 => 'website.com/Ă_ƒÂ¢',   2 => 1,   3 => 1, )

You can use my fix (because literally nothing that can be found in the internet is helpful in this case).

Continue reading Brutal fix for “Incorrect string value:” error in Matomo (Piwik)

E-mail extraction from an Outlook copied string

Sometimes you need to extract an e-mail address from a format like this:

Name Surname <[email protected]>

In such cases use this function:

/**
 * Extract mail address from a string
 * 
 * @param string $emailString
 * @return mixed match on success, false on error
 */
function extractEmail( $emailString ) {
    /** @var string $result **/
    $result = preg_match( '/^(?:[\w.\- ]+<)?([\w._-][email protected][\da-z\.-]+\.[a-z\.]{2,6})>?$/iu', $emailString, $matches );
                               
    if( $result === 1 || $result !== false ) {
        return $matches[1];
    } else {
        return false;
    }      
}

Link to test regular expression used by me is here.

Differences between character counting in PHP and Javascript

I’ve recently codded my own validation rules for forms. I’m checking length of a string on a client side and also on a server side (obviously it’s a good practice). After my script went on a production, users started to complain about validation errors (even if data in a form was passed proper). This is really strange but…
Continue reading Differences between character counting in PHP and Javascript