What potential pitfalls should PHP beginners be aware of when using preg_replace and substr functions together?

When using preg_replace and substr functions together, PHP beginners should be aware that the preg_replace function may return a string with different lengths, which can affect the accuracy of the substr function. To solve this issue, it's important to store the result of preg_replace in a variable and then use substr on that variable to ensure consistent string lengths.

$string = "Hello, World!";
$pattern = "/Hello/";
$replacement = "Hi";
$result = preg_replace($pattern, $replacement, $string);
$substring = substr($result, 0, 5);
echo $substring;