Code snippets

You will find here 19 code snippets I find useful so I don't have to Google ™ or Stackoverflow ™ them when I need. There are also code snippets I always forget! 😁 Sometimes, related links to the code will be available. The main difference with other random snippets you will find on the web is that in most cases (for the Symfony related ones) you will be able to run them directly from this website to check the output so you can verify it works as expected ™.

This snippet shows how to check if a string contains valid JSON with PHP. This function was introduced in PHP 8.3 to avoid using j...

PHP

Published on 2024-02-07 • Modified on 2024-02-07

 View the code


This snippet shows how to detect forbidden characters in a string with PHP. Here is one of the many possible solutions using the s...

PHP

Published on 2023-10-15 • Modified on 2023-10-15

 View the code


This snippet shows how to convert a string to an integer with PHP. Here, we want to extract the "5" from this URL. Problem: there...

PHP

Published on 2023-10-13 • Modified on 2023-10-13

 View the code


This snippet shows how to shuffle bytes with PHP and the randomizer class. We use the shufflebytes() function. This function does ...

PHP

Published on 2023-09-10 • Modified on 2023-09-10

 View the code


This snippet shows how to generate a random token for a mail confirmation link using PHP and Symfony. The final length of the gene...

PHP

Published on 2023-07-01 • Modified on 2023-07-01

 View the code


This snippet shows how to debug a variable with a concise stack trace with the Symfony VarDumper. The debug_print_backtrace() func...

Symfony

Published on 2023-04-28 • Modified on 2023-04-28

 View the code


This snippet shows how to use the Symfony string component to get the singular or plural of a word. It isn't easy to do this as th...

Symfony

Published on 2022-10-15 • Modified on 2022-10-15

 View the code


This snippet shows how to generate a slug with the Symfony string component. Usually, when using the Symfony string component, we ...

Symfony

Published on 2022-10-11 • Modified on 2022-10-11

 View the code


This snippet shows how to declare a string in PHP with a Heredoc and Nowdoc block. This one is for me as I always forget how to do...

PHP

Published on 2022-04-30 • Modified on 2022-04-30

 View the code


This snippet shows how to check if a string contains another string with PHP. This is a kind of tribute to our "old" PHP. The str...

PHP

Published on 2022-04-15 • Modified on 2022-04-15

 View the code