Other Functions
This file contains miscellaneous utility functions for various purposes.
Functions
1. debugPrint($data)
Prints a variable in a human-readable format.
Parameters:
mixed $data: The variable to print.
Usage:
debugPrint($_POST);
Example:
$data = ['name' => 'John', 'age' => 30];
debugPrint($data);
Output:
Array
(
[name] => John
[age] => 30
)
2. redirect($url)
Redirects to a different URL.
Parameters:
string $url: The URL to redirect to.
Usage:
redirect('https://example.com');
3. jsonResponse($data)
Sends a JSON response.
Parameters:
mixed $data: The data to send as JSON.
Usage:
jsonResponse(['status' => 'success']);
Example:
$data = ['status' => 'success', 'message' => 'Data processed successfully.'];
jsonResponse($data);
Output (HTTP Response):
{
"status": "success",
"message": "Data processed successfully."
}
4. getClientIp()
Gets the client's IP address.
Usage:
$ip = getClientIp();
Example:
$ip = getClientIp();
echo $ip;
Output:
192.168.1.1
5. getBrowserInfo()
Gets the client's browser information.
Usage:
$browserInfo = getBrowserInfo();
Example:
$browserInfo = getBrowserInfo();
echo $browserInfo;
Output:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36
6. sanitizeFilename($filename)
Converts a string to a safe filename.
Parameters:
string $filename: The input string.
Usage:
$safeFilename = sanitizeFilename('example image.png');
Example:
$filename = 'example image.png';
$safeFilename = sanitizeFilename($filename);
echo $safeFilename;
Output:
example_image.png
7. generateUUID()
Generates a UUID.
Usage:
$uuid = generateUUID();
Example:
$uuid = generateUUID();
echo $uuid;
Output:
550e8400-e29b-41d4-a716-446655440000
8. splitString($delimiter, $string)
Splits a string by a delimiter.
Parameters:
string $delimiter: The delimiter.string $string: The input string.
Usage:
$parts = splitString(',', 'apple,banana,cherry');
Example:
$string = 'apple,banana,cherry';
$delimiter = ',';
$parts = splitString($delimiter, $string);
print_r($parts);
Output:
Array
(
[0] => apple
[1] => banana
[2] => cherry
)
9. joinArray($delimiter, $array)
Joins an array into a string with a delimiter.
Parameters:
string $delimiter: The delimiter.array $array: The input array.
Usage:
$joined = joinArray(',', ['apple', 'banana', 'cherry']);
Example:
$array = ['apple', 'banana', 'cherry'];
$delimiter = ',';
$joined = joinArray($delimiter, $array);
echo $joined;
Output:
apple,banana,cherry
10. arrayGet(array $array, $key, $default = null)
Gets a value from an array or returns a default value.
Parameters:
array $array: The input array.mixed $key: The key to search for.mixed $default: The default value if the key is not found.
Usage:
$value = arrayGet($data, 'key', 'default');
Example:
$data = ['name' => 'John', 'age' => 30];
$value = arrayGet($data, 'name', 'Unknown');
echo $value;
Output:
John
11. sendEmail($to, $subject, $message, $headers = '')
Sends a simple email.
Parameters:
string $to: The recipient's email address.string $subject: The email subject.string $message: The email message.string $headers: Additional email headers.
Usage:
sendEmail('example@example.com', 'Subject', 'Message');
Example:
$to = 'example@example.com';
$subject = 'Test Email';
$message = 'This is a test email.';
$headers = 'From: webmaster@example.com';
sendEmail($to, $subject, $message, $headers);
12. isAssocArray(array $array)
Checks if an array is associative.
Parameters:
array $array: The array to check.
Usage:
$isAssoc = isAssocArray(['a' => 1, 'b' => 2]);
Example:
$array = ['a' => 1, 'b' => 2];
$isAssoc = isAssocArray($array);
var_dump($isAssoc);
Output:
bool(true)
13. getCurrentUrl()
Gets the current URL.
Usage:
$currentURL = getCurrentUrl();
Example:
$currentURL = getCurrentUrl();
echo $currentURL;
Output:
https://example.com/current/page
14. stripTagsExcept($text, array $allowedTags = [])
Strips HTML tags except specified ones.
Parameters:
string $text: The input text.array $allowedTags: The tags to allow.
Usage:
$cleanHtml = stripTagsExcept('<p>Hello <b>World</b></p>', ['<b>']);
Example:
$text = '<p>Hello <b>World</b></p>';
$allowedTags = ['<b>'];
$cleanHtml = stripTagsExcept($text, $allowedTags);
echo $cleanHtml;
Output:
Hello <b>World</b>
15. slugify($str)
Generates a URL slug from a string.
Parameters:
string $str: The input string.
Usage:
$slug = slugify('Hello World!');
Example:
$text = 'Hello World!';
$slug = slugify($text);
echo $slug;
Output:
hello-world