@HerTha PHP 8 made working with string functions a little more strict, but a quick fix is possible. Try using a null coalescing operator to tidy things up.
<?php
// Checks if a non-null value exists for a variable, if it does then it will be passed to trim(), if it doesn't or it has a null value, will fall back to an empty string.
trim($someVar ?? '');
// This should work with preg_replace() as well, or any function that expects a string but may not receive one
preg_replace('/someregexstuff/', 'Replacement', $someVar ?? '');
Hope that helps out!