Cheat Sheets

Text

Markdown:

  • asdf

Social:

  • Acronyms:
    • https://learn.getgrav.org/content/media/acronyms.txt
  • Emoji Cheat Sheet, valid for Git Markdown

Linux

  • mkdir
    • create directory
  • mv
    • move or rename
  • cp -r -f
    • copy
  • ls -la
    • list hidden files and permissions (sometimes ll does not exist)

network

  • ifconfig eth0 down
  • ifconfig eth0 123.123.123.234 netmask 255.255.255.0 up
  • route add default gw 123.123.123.1
  • ifup -a
  • ifdown -a

Dev

PHP

Versions
  • since 7.0.0: introduced NULL coalesce operator ??. It is stackable.
    • $expr1 ?? $expr2 ?? $expr3 returns the first non-false expression.
  • since 5.3.0: It is allowed to drop the php closing tag ?>.
    • this is recommended for php only files as it prevents unintentional printing of space or control characters e.g. when including such files.
  • since 5.3.0: The tenary operator can be used in a short form expr1?:expr3.
    • no expr2 needed. returns expr1 if it validates to true.
Various Examples

Javascript

Ajax Examples
Replace all occurrences of a string

instead of only the first (which is the default behaviour of str = str.replace()).

This is achieved by using RegEx with the global flag. However this means to take extra care of RegEx special chars in the find text (needle).

Solution from Stackoverflow:

  1. Helper function as written in MDN.
    function escapeRegExp(str) {
    return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
    }
  2. Usage with global (g) flag. Use (i) additionally for case insensitive search.
    var str = '<p>any|other text, any occurrence</p>';
    str = str.replace(new RegExp(escapeRegExp("any"), 'g'), "another");
Showing objects in the JavaScript console without going mad

Covered by Code Maven.

You can "print" JavaScript objects to the browser console via console.log('Something:', myobject). Some browsers do not print the result, but reference the view to it. If the object is changed during runtime, only the last version will be displayed for each console.log call, no matter where you position the calls in your code. [Actually, you might circumvent this by adding wait time during the different calls or set breakpoints but that seems tedious under most circumstances.]

Solution: Convert the object to a string and back before printing it.

var myObject = {
    'id' : 1,
    'name' : 'Test'
};
console.log("My Object: ", JSON.parse(JSON.stringify(myObject)));