Format Functions Documentation
This file contains functions related to formatting, including currency formatting, string manipulation, and HTML sanitization.
formatCurrency
Formats a number as currency.
Usage
$formatted = formatCurrency(1234.56, '$');
echo $formatted; // Output: $1,234.56
Parameters
float $amount- The amount to format.string $currency- The currency symbol (default is$).
Returns
string- The formatted currency.
formatDate
Formats a date to a specified format.
Usage
$formattedDate = formatDate('2024-07-30 13:49:26', 'F j, Y, g:i a');
echo $formattedDate; // Output: July 30, 2024, 1:49 pm
Parameters
string $date- The date string to format.string $format- The desired date format (default isY-m-d H:i:s).
Returns
string- The formatted date.
formatSizeUnits
Formats a number of bytes into a human-readable format.
Usage
$size = formatSizeUnits(1024);
echo $size; // Output: 1 KB
Parameters
int $bytes- The number of bytes to format.
Returns
string- The formatted size.
formatPhoneNumber
Formats a phone number into a standard format.
Usage
$phoneNumber = formatPhoneNumber('1234567890');
echo $phoneNumber; // Output: (123) 456-7890
Parameters
string $number- The phone number to format.
Returns
string- The formatted phone number.
sanitizeFilename
Converts a string to a safe filename.
Usage
$safeFilename = sanitizeFilename('example image.png');
echo $safeFilename; // Output: example_image.png
Parameters
string $filename- The input string.
Returns
string- The sanitized filename.
escapeHtml
Escapes special characters to HTML entities.
Usage
$html = escapeHtml('<div>"Hello"</div>');
echo $html; // Output: <div>"Hello"</div>
Parameters
string $string- The input string.
Returns
string- The escaped string.
stripTagsExcept
Strips HTML tags except specified ones.
Usage
$cleanHtml = stripTagsExcept('<p>Hello <b>World</b></p>', ['<b>']);
echo $cleanHtml; // Output: Hello <b>World</b>
Parameters
string $text- The input text.array $allowedTags- The tags to allow.
ΒΆReturns
string- The sanitized string.