Skip to content

Instantly share code, notes, and snippets.

View gabrielbarros's full-sized avatar

Gabriel Barros gabrielbarros

  • Rio de Janeiro, Brasil
  • 23:47 (UTC -03:00)
View GitHub Profile
@gabrielbarros
gabrielbarros / replace-date-time-mktime-with-datetime-class.php
Created March 19, 2024 20:48
Replace date, gmdate, time, mktime, gmmktime with DateTime class
<?php
// date
echo "date\n";
echo date('Y-m-d H:i:s'), "\n";
$dt = new DateTime();
echo $dt->format('Y-m-d H:i:s'), "\n\n";
// gmdate (UTC date)
echo "gmdate\n";
@gabrielbarros
gabrielbarros / list-bytes-and-codepoints.php
Last active March 11, 2024 17:06
List bytes and codepoints in PHP
<?php
/*
* Heart emoji: ❤️
* UTF-8 bytes: E2 9D A4 EF B8 8F
* Code points: U+2764 U+FE0F
*/
header('Content-Type: text/plain');
@gabrielbarros
gabrielbarros / latin1-utf8.php
Created March 8, 2024 14:10
Convert from ISO-8859-1 to UTF-8 and vice-versa; test if it's valid UTF-8
<?php
// ISO-8859-1 → UTF-8
function latin1ToUtf8(string $str): string
{
return iconv('ISO-8859-1', 'UTF-8', $str);
}
// UTF-8 → ISO-8859-1
function utf8ToLatin1(string $str): string
@gabrielbarros
gabrielbarros / unicode-and-byte-notation.md
Last active March 11, 2024 17:07
Unicode and byte notation in various places and programming languages

Unicode and byte notation

Letter: A
Code points: U+0041
UTF-8 bytes: 41
UTF-16BE bytes: 00 41

Pound sign: £
Code points: U+00A3
@gabrielbarros
gabrielbarros / counting-utf8-bytes-code-points-and-grapheme-clusters.md
Last active March 11, 2024 20:28
Counting UTF-8 bytes, code points and grapheme clusters in various programming languages

Counting UTF-8 bytes, code points and grapheme clusters

Strings to test:

• Star emoji
⭐

- UTF-8 bytes: 3
- code points: 1
@gabrielbarros
gabrielbarros / reverse-string.php
Last active March 11, 2024 17:08
Reverse a string correctly in PHP considering bytes, code points and graphemes
<?php
// For ASCII only
$hello = 'Hello';
$revStrBytes1 = '';
for ($i = strlen($hello) - 1; $i >= 0; $i--) {
$revStrBytes1 .= $hello[$i];
}
// Alternative to the code above
@gabrielbarros
gabrielbarros / mail-servers.txt
Last active March 7, 2024 20:57
Mail servers (SMTP, IMAP and POP) for Gmail and Microsoft (Hotmail, Outlook)
Gmail
IMAP: imap.gmail.com:993 (SSL/TLS)
POP: pop.gmail.com:995 (SSL/TLS)
SMTP: smtp.gmail.com:587 (STARTTLS)
Microsoft (Hotmail, Outlook)
IMAP: outlook.office365.com:993 (SSL/TLS)
POP: outlook.office365.com:995 (SSL/TLS)
SMTP: smtp-mail.outlook.com:587 (STARTTLS)
@gabrielbarros
gabrielbarros / install-rclone
Created March 6, 2024 20:09
Install and update rclone
#!/usr/bin/env bash
set -euo pipefail
echo 'Installing rclone...'
wget https://downloads.rclone.org/rclone-current-linux-amd64.zip
unzip rclone-current-linux-amd64.zip
sudo mv rclone-*-linux-amd64/rclone /usr/local/bin
@gabrielbarros
gabrielbarros / install-mkcert
Created March 6, 2024 20:09
Install and update mkcert
#!/usr/bin/env bash
set -euo pipefail
echo 'Installing mkcert...'
curl -s https://api.github.com/repos/FiloSottile/mkcert/releases/latest | \
jq -r '.assets[].browser_download_url' | grep -F linux-amd64 | wget -i -
sudo mv mkcert-* /usr/local/bin/mkcert
@gabrielbarros
gabrielbarros / install-diff-so-fancy
Created March 6, 2024 20:09
Install and update diff-so-fancy
#!/usr/bin/env bash
set -euo pipefail
echo 'Installing diff-so-fancy...'
curl -s https://api.github.com/repos/so-fancy/diff-so-fancy/releases/latest | \
jq -r '.assets[].browser_download_url' | wget -i -
sudo mv diff-so-fancy /usr/local/bin