This content originally appeared on DEV Community and was authored by shoaiyb sysa
STR_SPRINTF
An alternative to the default function sprintf
<?php
function _sprintf(string $format, mixed ...$values): string {
foreach($values as $key => $value) {
$format = str_replace('%' . $key, $value, $format);
}
return $format;
}
?>
Example:
<?php
echo _sprintf('I am %0 and so %1 now!', 'tired', 'hungry'); // returns: I am tired and so hungry now!
?>
_SEARCH
What I use to add search functionality to my php sites!
<?php
function _search(string $search, array $in): array|string {
$_search = strtolower($search);
foreach($in as $key => $value) {
$_value = strtolower($value);
if(str_contains($_value, $_search)) {
$result[$key] = $value;
}
}
if(isset($result) && is_array($result)) {
return $result;
} else {
return 'Ohh no! No search result have been found!';
}
}
?>
Example:
<?php
print_r(_search('Old', ['New Post', 'Old Post', 'New Page', 'Old Page']));
?>
This content originally appeared on DEV Community and was authored by shoaiyb sysa

shoaiyb sysa | Sciencx (2021-11-21T07:46:06+00:00) I’ll Be Writing PHP Functions In Here Just For Fun!. Retrieved from https://www.scien.cx/2021/11/21/ill-be-writing-php-functions-in-here-just-for-fun/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.