Byt Wiki

Other Functions

This file contains miscellaneous utility functions for various purposes.

Functions

1. debugPrint($data)

Prints a variable in a human-readable format.

Parameters:

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:

Usage:

redirect('https://example.com');

3. jsonResponse($data)

Sends a JSON response.

Parameters:

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:

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:

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:

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:

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:

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:

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:

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:

Usage:

$slug = slugify('Hello World!');

Example:

$text = 'Hello World!';
$slug = slugify($text);
echo $slug;

Output:

hello-world