How can PHP functions like substr() and wordwrap() be utilized to format strings effectively?

When working with strings in PHP, functions like substr() can be used to extract a specific portion of a string, while wordwrap() can be used to wrap a string to a specific number of characters per line. These functions can be utilized to format strings effectively, such as truncating long strings or breaking them into multiple lines for better readability.

// Using substr() to truncate a string
$string = "This is a long string that needs to be truncated.";
$truncatedString = substr($string, 0, 20); // Truncate to 20 characters
echo $truncatedString;

// Using wordwrap() to wrap a string
$string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
$wrappedString = wordwrap($string, 30, "<br>"); // Wrap at 30 characters per line
echo $wrappedString;