HelPHP

Utils
in package

Tags
class

Utils

Provides utility functions for system operations, string and array manipulation, file and process management, configuration updates, and more. Includes helpers for launching and tracking system processes, logging, filtering strings, computing discounts and VAT, merging arrays, checking URLs, generating random strings, handling CSS and configuration, and retrieving IP addresses.

In fact every little utils that haven't their places in other libs, but they are all usefull.

they are grouped in different sections (search for them to jump on it): -SYSTEM SECTION -NETWORK SECTION -CONFIG SECTION -MIME SECTION -STRING MANIPULATION SECTION -NUMBERS MANIPULATION SECTION -ARRAY MANIPULATION SECTION

Table of Contents

Methods

__construct()  : mixed
array_merge_by_value()  : array<string|int, mixed>
merge two associative array by a common value key
array_str_in_string()  : bool
Checks if any string from an array is present in a given string.
array_to_csv()  : void
Converts an array to CSV and writes to a file or outputs as attachment.
check_url()  : bool
Checks if a URL is reachable (HTTP status < 400).
compute_discount()  : float
Computes the discounted price.
compute_vat()  : float
Computes the price including VAT.
filter_string()  : string
Filters a string to only allow characters from a given set.
fix_EOL()  : string
Fixes double slaches in the classic EOL.
follow_system_process()  : string|false
Follows the progress of a system process tracked in session.
follow_system_process_redis()  : mixed
Follows the progress of a system process tracked in Redis.
get_css_as_str()  : string
Returns a CSS theme in DB as a string to export it.
get_ext_from_mime()  : mixed
get_host_ip()  : string
Retrieves the host IP address from the routing table.
get_ip()  : string
Retrieves the client IP address.
get_ip_v6()  : string
Retrieves the client IPv6 address.
get_mime_type()  : mixed
make_constant()  : bool
Regenerates the constants.js file from PHP constants.
random_string()  : string
Generates a random string of given length and character set.
remove_accents()  : string
Removes accents from a string.
remove_vat()  : float
Removes VAT from a price.
str_contains_only()  : bool
Checks if a string contains only characters from a given set.
string_in_array_str()  : mixed
Checks if a string is present at the start of any string in an array.
substr_from_html()  : string
Gets a substring from HTML, preserving tags and word boundaries.
system_process()  : void
Launches a system process and pushes its output to a session variable, which can be followed by a key.
system_process_no_session()  : string
Launches a system process and returns its output as a string (without session tracking).
system_process_no_tracking()  : void
Launches one or more system processes with no tracking. The script wait for the end of the command to continue.
system_process_with_tracking()  : void
Launches one or more system processes and tracks their output/progress. The command is executed in parallel and the script directly continue.
update_htaccess()  : bool
Rewrites the generated blocks of the instance .htaccess: the RewriteBase (the folder the site is served from) and the http / host redirections. Both belong to the config, not to a file edited by hand, and everything outside the two blocks is left untouched.
write_in_config()  : bool
Updates the main config file with new variable values.

Methods

__construct()

public __construct() : mixed

array_merge_by_value()

merge two associative array by a common value key

public static array_merge_by_value(array<string|int, mixed> $a, array<string|int, mixed> $b, string $valueKey) : array<string|int, mixed>

The two associative array should have a common label key that will be used to merge them if I put the valueKey "id", my two array will be merged by the id key in their values. Preserve the orginal index in case of identical key, the second one is kept

Exemple: $a = [['id'=>'1','value'=>'jesuis1'],['id'=>'2','value'=>'jesuis2']]; $b = [['id'=>'1','value'=>'jesuispas1'],['id'=>'3','value'=>'jesuis3']]; $c = array_merge_by_value($a, $b, 'id'); [['id'=>'1','value'=>'jesuispas1'],['id'=>'2','value'=>'jesuis2'],['id'=>'3','value'=>'jesuis3']] $c = array_merge_by_value($b, $a, 'id'); [['id'=>'1','value'=>'jesuis1'],['id'=>'2','value'=>'jesuis2'],['id'=>'3','value'=>'jesuis3']]

Parameters
$a : array<string|int, mixed>

First array.

$b : array<string|int, mixed>

Second array.

$valueKey : string

Key to merge by.

Return values
array<string|int, mixed> —

Merged array.

array_str_in_string()

Checks if any string from an array is present in a given string.

public static array_str_in_string(string $str, array<string|int, mixed> $array, bool $startwith) : bool
Parameters
$str : string

The string to search in.

$array : array<string|int, mixed>

The array of needles.

$startwith : bool

If true, checks for start of string.

Return values
bool —

True if found, false otherwise.

array_to_csv()

Converts an array to CSV and writes to a file or outputs as attachment.

public static array_to_csv(array<string|int, mixed> $arraydata, string $filename[, bool $attachment = false ][, bool $headers = true ]) : void
Parameters
$arraydata : array<string|int, mixed>

Data to write.

$filename : string

Filename or output stream.

$attachment : bool = false

If true, sends as attachment.

$headers : bool = true

If true, includes headers.

check_url()

Checks if a URL is reachable (HTTP status < 400).

public static check_url(string $url) : bool
Parameters
$url : string

The URL to check.

Return values
bool —

True if reachable, false otherwise.

compute_discount()

Computes the discounted price.

public static compute_discount(float $prix, float $remise) : float
Parameters
$prix : float

Original price.

$remise : float

Discount percentage.

Return values
float —

Discounted price.

compute_vat()

Computes the price including VAT.

public static compute_vat(float $prix, float $tva) : float
Parameters
$prix : float

Original price.

$tva : float

VAT percentage.

Return values
float —

Price including VAT.

filter_string()

Filters a string to only allow characters from a given set.

public static filter_string(string $t[, string $limit = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_" ]) : string
Parameters
$t : string

The input string.

$limit : string = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"

Allowed characters.

Return values
string —

The filtered string.

fix_EOL()

Fixes double slaches in the classic EOL.

public static fix_EOL(string $str) : string
Parameters
$str : string

The input string.

Return values
string —

The string with fixed EOL.

follow_system_process()

Follows the progress of a system process tracked in session.

public static follow_system_process([string|false $key = false ]) : string|false
Parameters
$key : string|false = false

The key referencing the process.

Return values
string|false —

The process output or 'ok!' when finished.

follow_system_process_redis()

Follows the progress of a system process tracked in Redis.

public static follow_system_process_redis([string|false $key = false ][, bool $update_history = false ]) : mixed
Parameters
$key : string|false = false

The key referencing the process.

$update_history : bool = false

Whether to update history when finished.

Return values
mixed —

Progress percentage, 'ok!' when finished, or false.

get_css_as_str()

Returns a CSS theme in DB as a string to export it.

public static get_css_as_str([int $id_theme = 0 ]) : string
Parameters
$id_theme : int = 0

Theme ID.

Return values
string —

CSS string.

get_ext_from_mime()

public static get_ext_from_mime(mixed $mime) : mixed
Parameters
$mime : mixed

get_host_ip()

Retrieves the host IP address from the routing table.

public static get_host_ip() : string
Return values
string —

The host IP address.

get_ip()

Retrieves the client IP address.

public static get_ip([int $flags = 0 ]) : string

The declared address is trusted without condition (X-Real-IP, then X-Forwarded-For), then REMOTE_ADDR is used. Security::client_ip() is the identity used by the recent paths (MCP, REST): it honours Config::AUTH_STRICT_IP. This one stays as it is, because users_connections stores what it returned.

Parameters
$flags : int = 0

A FILTER_FLAG_* mask, FILTER_FLAG_IPV6 for the IPv6 form.

Return values
string —

The IP address or 'NoIp' if not found.

get_ip_v6()

Retrieves the client IPv6 address.

public static get_ip_v6() : string

Note: the ip column of db try_logs connection table must be >= 50 to store an IPv6.

Return values
string —

The IPv6 address or 'NoIp' if not found.

get_mime_type()

public static get_mime_type(mixed $ext) : mixed
Parameters
$ext : mixed

make_constant()

Regenerates the constants.js file from PHP constants.

public static make_constant() : bool
Return values
bool —

True on success, false on error.

random_string()

Generates a random string of given length and character set.

public static random_string([int $length = 5 ][, string|false $valid_characters = false ]) : string
Parameters
$length : int = 5

Length of the string.

$valid_characters : string|false = false

Allowed characters.

Return values
string —

Random string.

remove_accents()

Removes accents from a string.

public static remove_accents(string $text) : string
Parameters
$text : string

The input string.

Return values
string —

The string without accents.

remove_vat()

Removes VAT from a price.

public static remove_vat(float $prix, float $tva) : float
Parameters
$prix : float

Price including VAT.

$tva : float

VAT percentage.

Return values
float —

Price without VAT.

str_contains_only()

Checks if a string contains only characters from a given set.

public static str_contains_only(string $string, string $gama) : bool
Parameters
$string : string

The string to check.

$gama : string

Allowed characters.

Return values
bool —

True if only allowed characters, false otherwise.

string_in_array_str()

Checks if a string is present at the start of any string in an array.

public static string_in_array_str(string $str, array<string|int, mixed> $array[, bool $details = true ]) : mixed
Parameters
$str : string

The string to search for.

$array : array<string|int, mixed>

The array to search in.

$details : bool = true

If true, returns matching entries; else returns true/false.

Return values
mixed —

Array of matches or boolean.

substr_from_html()

Gets a substring from HTML, preserving tags and word boundaries.

public static substr_from_html(string $str, int $start, int $len) : string
Parameters
$str : string

The HTML string.

$start : int

Start position.

$len : int

Length of substring.

Return values
string —

The substring with HTML tags preserved.

system_process()

Launches a system process and pushes its output to a session variable, which can be followed by a key.

public static system_process(string $cmd, string $key) : void

Uses proc_open to execute the command

Parameters
$cmd : string

The command to execute.

$key : string

The key to identify the process output in session.

system_process_no_session()

Launches a system process and returns its output as a string (without session tracking).

public static system_process_no_session(string $cmd) : string

Uses proc_open to execute the command

Parameters
$cmd : string

The command to execute.

Return values
string —

The output of the command.

system_process_no_tracking()

Launches one or more system processes with no tracking. The script wait for the end of the command to continue.

public static system_process_no_tracking(array<string|int, mixed>|string $cmds, string $key[, string|false $type = false ][, array<string|int, mixed>|false $arg = false ][, array<string|int, mixed>|false $to_unlocked = false ]) : void
Parameters
$cmds : array<string|int, mixed>|string

The command(s) to execute.

$key : string

The key to reference the process in Redis.

$type : string|false = false

Optional. Type for progress tracking.

$arg : array<string|int, mixed>|false = false

Optional. Argument for the command type.

$to_unlocked : array<string|int, mixed>|false = false

Optional. Paths to lock/unlock during the process.

system_process_with_tracking()

Launches one or more system processes and tracks their output/progress. The command is executed in parallel and the script directly continue.

public static system_process_with_tracking(array<string|int, mixed>|string $cmds, string $key[, string|false $type = false ][, array<string|int, mixed>|false $arg = false ][, array<string|int, mixed>|false $to_unlocked = false ]) : void

Will try to store the progress of the command in Redis.

Parameters
$cmds : array<string|int, mixed>|string

The command(s) to execute.

$key : string

The key to reference the process in Redis.

$type : string|false = false

Optional. Type for progress tracking.

$arg : array<string|int, mixed>|false = false

Optional. Argument for the command type.

$to_unlocked : array<string|int, mixed>|false = false

Optional. Paths to lock/unlock during the process.

update_htaccess()

Rewrites the generated blocks of the instance .htaccess: the RewriteBase (the folder the site is served from) and the http / host redirections. Both belong to the config, not to a file edited by hand, and everything outside the two blocks is left untouched.

public static update_htaccess([string|bool $path = false ][, array<string|int, mixed> $overrides = [] ]) : bool

The redirections are driven by two optional constants: Config::FORCE_HTTPS (bool) and Config::WWW_MODE ('keep' by default, 'add' or 'strip'). An instance that defines neither keeps the behaviour it had before, that is no redirection at all.

Parameters
$path : string|bool = false

Path of the .htaccess, defaults to the instance one.

$overrides : array<string|int, mixed> = []

Values written by the caller in this very request: the config constants are read at bootstrap, so a save would otherwise generate from stale ones.

Return values
bool —

true when the file has been written.

write_in_config()

Updates the main config file with new variable values.

public static write_in_config(string|array<string|int, mixed> $variable_name, string|array<string|int, mixed> $type, mixed $value[, string $path = false ]) : bool
Parameters
$variable_name : string|array<string|int, mixed>

Variable name(s) to update.

$type : string|array<string|int, mixed>

Type(s) of variable ('s' for string, etc.).

$value : mixed

Value(s) to set.

$path : string = false

Path of the config file to modify.

Return values
bool —

True on success, false on error.

On this page

Search results