I’ll Be Writing PHP Functions In Here Just For Fun!

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);
}
retu…


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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » I’ll Be Writing PHP Functions In Here Just For Fun!." shoaiyb sysa | Sciencx - Sunday November 21, 2021, https://www.scien.cx/2021/11/21/ill-be-writing-php-functions-in-here-just-for-fun/
HARVARD
shoaiyb sysa | Sciencx Sunday November 21, 2021 » I’ll Be Writing PHP Functions In Here Just For Fun!., viewed ,<https://www.scien.cx/2021/11/21/ill-be-writing-php-functions-in-here-just-for-fun/>
VANCOUVER
shoaiyb sysa | Sciencx - » I’ll Be Writing PHP Functions In Here Just For Fun!. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/21/ill-be-writing-php-functions-in-here-just-for-fun/
CHICAGO
" » I’ll Be Writing PHP Functions In Here Just For Fun!." shoaiyb sysa | Sciencx - Accessed . https://www.scien.cx/2021/11/21/ill-be-writing-php-functions-in-here-just-for-fun/
IEEE
" » I’ll Be Writing PHP Functions In Here Just For Fun!." shoaiyb sysa | Sciencx [Online]. Available: https://www.scien.cx/2021/11/21/ill-be-writing-php-functions-in-here-just-for-fun/. [Accessed: ]
rf:citation
» I’ll Be Writing PHP Functions In Here Just For Fun! | shoaiyb sysa | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.