<?php
/**
* Limits the output of strings to prevent excessive data and aid in sandbox usage.
*
* @param string $string The string to limit.
* @param int $maxLength The maximum length of the output string. Defaults to 100 characters.
* @return string The limited string.
*/
function limitStringOutput(string $string, int $maxLength = 100): string
{
// Use mb_substr for multi-byte string support.
$limitedString = mb_substr($string, 0, $maxLength, 'UTF-8');
// If the string is longer than the maximum length, add "..." to indicate truncation.
if (mb_strlen($string, 'UTF-8') > $maxLength) {
$limitedString .= "...";
}
return $limitedString;
}
// Example usage (for testing):
$longString = "This is a very long string that needs to be limited for sandbox testing purposes. It should be truncated if it exceeds a certain length.";
$limitedString = limitStringOutput($longString, 50);
echo $limitedString . "\n"; // Output: This is a very long string that needs to be limited for sandbox testing purposes.
$shortString = "Short string";
$limitedShortString = limitStringOutput($shortString, 50);
echo $limitedShortString . "\n"; //Output: Short string
?>
Add your comment