📖Usage

This page explains the functions structure and examples.

In this section, you'll find detailed information on how to use each helper provided by Helpify. If you haven't installed the package yet, make sure to follow the installation instructions first.

  1. short_url

    Shortens a given URL using the is.gd URL shortening service.

use Helpify\Helpers;

$url = 'https://www.example.com';
$shortenedURL = Helpers::short_url($url);
//---------------
$shortenedURL = short_url($url); // https://is.gd/dsUwVY" 

Parameters

  • $url (string): The URL to shorten.

Return Value

  • (string): The shortened URL.

  1. checkValidIpAddress

Checks if a given IP address is valid.

use Helpify\Helpers;
$ip = '192.168.1.1'; 

$isIPAddressValid = checkValidIpAddress($ip);
// OR
$isIPAddressValid = Helpers::checkValidIpAddress($ip);

Parameters

  • $ip (string): The IP address to check.

Return Value

  • (bool): true if the IP address is valid, false otherwise.

  1. getClientIp

Gets the client's IP address from various possible sources, such as HTTP headers and server variables.

use Helpify\Helpers;

$clientIpAddress = getClientIp();
//OR
$clientIpAddress = Helpers::getClientIp();

Return Value

  • (string): The client's IP address.

  1. getClientBrowser

Gets the client's browser based on the User-Agent header.

use Helpify\Helpers;

$clientBrowser = getClientBrowser();
//OR
$clientBrowser = Helpers::getClientBrowser();

Return Value

  • (string): The client's browser.

  1. htmlTag

Generates an HTML tag with attributes

use Helpify\Helpers;

$tag = 'div';
$attributes = ['class' => 'container', 'id' => 'main-container'];
$content = 'Hello, World!';

$htmlTag = htmlTag($tag, $attributes, $content);
//OR
$htmlTag = Helpers::htmlTag($tag, $attributes, $content);

Parameters

  • $tag (string): The tag name.

  • $attributes (array, optional): The attributes of the tag.

  • $content (string, optional): The content of the tag.

Return Value

  • (string): The generated HTML tag.

  1. getClientOs

Gets the client's operating system based on the User-Agent header.

use Helpify\Helpers;

$clientOs = getClientOs();
//OR
$clientOs = Helpers::getClientOs();

Return Value

  • (string): The client's operating system.

  1. getClientDevice

Gets the type of device used by the client based on the User-Agent header.

use Helpify\Helpers;

$clientDevice = getClientDevice();
//OR
$clientDevice = Helpers::getClientDevice();

Return Value

  • (string): The client's device.

  1. getClientCountry

Gets the client's country based on their IP address.

use Helpify\Helpers;

$clientCountry = getClientCountry();
//OR
$clientCountry = Helpers::getClientCountry();

Return Value

  • (string): The client's country.

  1. activeClass

Get the CSS class for the active route.

use Illuminate\Support\Facades\Route;
use Helpify\Helpers;

$routes = ['home', 'about', 'contact'];
$outputClass = 'active';

$cssClass = activeClass($routes, $outputClass);
//OR
$cssClass = Helpers::activeClass($routes, $outputClass);

Parameters

  • $routes (array): The routes to check for activity.

  • $output (string, optional): The output class to return if a match is found (default: "active").

Return Value

  • (string|null): The output class if the current route matches any of the provided routes, otherwise null.

  1. formatDate

Formats a date in the specified format.

use Helpify\Helpers;

$format = 'Y-m-d H:i:s';
$date = new \DateTime();

$formattedDate = formatDate($format, $date);
//OR
$formattedDate = Helpers::formatDate($format, $date);

Parameters

  • $format (string): The date format (e.g., 'Y-m-d H:i:s').

  • $date (\DateTimeInterface): The DateTime object to format.

Return Value

  • (string): The formatted date.

  1. truncateString

Truncates a string to a specified length and appends '...' if the string is longer than the specified length.

use Helpify\Helpers;

$string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
$length = 20;

$truncatedString = truncateString($string, $length);
//OR
$truncatedString = Helpers::truncateString($string, $length);

Parameters

  • $string (string): The string to truncate.

  • $length (int): The desired length.

Return Value

  • (string): The truncated string.

  1. generateSlug

Generates a URL-friendly slug from a given string by replacing non-alphanumeric characters with hyphens.

use Helpify\Helpers;

$string = 'Hello, World!';

$slug = generateSlug($string);
//OR
$slug = Helpers::generateSlug($string);

Parameters

  • $string (string): The string to convert to a slug.

Return Value

  • (string): The generated slug.

  1. generateRandomString

Generates a random string of a specified length.

use Helpify\Helpers;

$length = 8;

$randomString = generateRandomString($length);
//OR
$randomString = Helpers::generateRandomString($length);

Parameters

  • $length (int, optional): The length of the random string (default: 10).

Return Value

  • (string): The generated random string.

  1. arrayFlatten

Flattens a multi-dimensional array into a single level.

use Helpify\Helpers;

$array = [1, [2, [3, 4]]];

$flattenedArray = arrayFlatten($array);
//OR
$flattenedArray = Helpers::arrayFlatten($array);

Parameters

  • $array (array): The array to flatten.

Return Value

  • (array): The flattened array.

Generates an HTML link with the target attribute set for external link

use Helpify\Helpers;

$url = 'https://www.example.com';
$text = 'Visit Example';

$htmlLink = externalLink($url, $text);
//OR
$htmlLink = Helpers::externalLink($url, $text);

Parameters

  • $url (string): The URL of the link.

  • $text (string): The anchor text of the link.

Return Value

  • (string): The HTML link.

  1. seededShuffle

Shuffles an array using a seeded random number generator for reproducibility.

use Helpify\Helpers;

$array = [1, 2, 3, 4, 5];

$shuffledArray = seededShuffle($array);
//OR
$shuffledArray = Helpers::seededShuffle($array);

Parameters

  • $array (array): The array to shuffle.

Return Value

  • (array): The shuffled array.

  1. generateModelSlug

Generates a slug for a Laravel model instance using the specified field.

use Helpify\Helpers;

$model = new \App\Models\Post(['name' => 'Example Post']);
$field = 'name';

$modelSlug = generateModelSlug($model, $field);
//OR
$modelSlug = Helpers::generateModelSlug($model, $field);

Parameters

  • $model (\Illuminate\Database\Eloquent\Model): The Eloquent model instance.

  • $field (string, optional): The field to use for generating the slug (default: 'name').

Return Value

  • (string): The generated slug.

  1. writeToEnvFile

Add or update a key-value pair in the .env file.

use Helpify\Helpers;

$key = 'APP_ENV';
$value = 'production';

writeToEnvFile($key, $value);
//OR
Helpers::writeToEnvFile($key, $value);

Parameters

  • $key (string): The key to add or update.

  • $value (string): The value to set for the key.

Return Value

  • (void)

  1. addNewLineToEnvFile

Add a new line to the .env file.

use Helpify\Helpers;

$key = 'NEW_KEY';
$value = 'new_value';

addNewLineToEnvFile($key, $value);
//OR
Helpers::addNewLineToEnvFile($key, $value);

Parameters

  • $key (string): The key to add or update.

  • $value (string): The value to set for the key.

Return Value

  • (void)

  1. toCamelCase

Converts a string to camel case.

use Helpify\Helpers;

$string = 'hello_world';

$camelCaseString = toCamelCase($string);
//OR
$camelCaseString = Helpers::toCamelCase($string);

Parameters

  • $string (string): The string to convert.

Return Value

  • (string): The camel case version of the string.

Last updated

Was this helpful?