PHP code example of repat / php-helper
1. Go to this page and download the library: Download repat/php-helper library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
repat / php-helper example snippets
$arr1 = [1, 2, 3];
$arr2 = [3, 2, 1];
array_equal($arr1, $arr2);
// returns: true
$arr3 = [4, 5, 6];
array_equal($arr1, $arr3);
// returns: false
$array = [1, 3, 5];
print_r(array_key2value($array));
// returns: Array( [1] => 1, [3] => 3, [5] => 5 )
$array = ['foo', 'bar'];
print_r(array_delete_value($array, 'foo'));
// returns Array( [1] => "bar" )
contains_duplicates([1, 1]);
// returns: true
contains_duplicates([1, 2]);
// returns: false
$array = [
'bar' => 'foo',
'sub' => [
'some' => 'thing',
],
];
$keys = [
'bar' => 'biz', // change all 'bar' keys to 'biz' keys
'some' => 'any',
];
array_change_keys($array, $keys);
// returns:[
// 'biz' => 'foo',
// 'sub' => [
// 'any' => 'thing',
// ],
// ];
array_key_replace(['bar' => 'foo'], 'bar', 'bizz');
// returns : ['bizz' => 'foo']
array_avg([1, 2, 3]);
// returns : 2
array_avg([]);
// returns : null
days_in_month();
// returns: 31 (for e.g. May)
days_in_month($april = 4);
// returns: 30
days_in_month($feb = 2, $year = 2020);
// returns: 29 (2020 is a leap year)
days_this_month();
// returns: 31 (for e.g. May)
days_next_month();
// returns: 30 (for e.g. May because June has 30)
days_this_year();
// returns: 365 (because it's not a leap year)
days_left_in_month();
// returns: 29 (on 1st April)
days_left_in_year();
// returns: 274 (on 1st April 2019)
timezone_list();
// returns:
// [
// "Pacific/Pago_Pago" => "(UTC-11:00) Pacific/Pago_Pago",
// "Pacific/Niue" => "(UTC-11:00) Pacific/Niue",
// "Pacific/Midway" => "(UTC-11:00) Pacific/Midway",
// ...
// "Pacific/Chatham" => "(UTC+13:45) Pacific/Chatham",
// "Pacific/Kiritimati" => "(UTC+14:00) Pacific/Kiritimati",
// "Pacific/Apia" => "(UTC+14:00) Pacific/Apia",
// ];
tomorrow();
// returns: Carbon\Carbon @1554156000 {#5618
// date: 2019-04-20 00:00:00.0 Europe/Amsterdam (+02:00),
// }
yesterday();
// returns: Carbon\Carbon @1554156000 {#5618
// date: 2019-04-19 00:00:00.0 Europe/Amsterdam (+02:00),
// }
seconds2minutes(42);
// returns: 00:42
seconds2minutes(90);
// returns: 01:30
seconds2minutes(4223);
// returns: 70:23
diff_in_days('2018-04-19', '2018-04-21');
// returns: 2
diff_in_days(today(), yesterday());
// returns: 1
use App\Models\User;
object2array(User::first());
// returns: [
// "casts" => [
// "someday_at" => "datetime",
// // ...
// ],
// "incrementing" => true,
// "exists" => true,
// "wasRecentlyCreated" => false,
// "timestamps" => true,
// ]
filepath2fqcn('/Users/john/code/app/Models/User.php', '/Users/john/code/');
// returns: App\Models\User
filepath2fqcn('/Users/john/code/app/Models/User.php', '/Users/john/code');
// returns: App\Models\User
filepath2fqcn('app/Models/User.php');
// returns: App\Models\User
filepath2fqcn('/Users/john/code/app/Models/User.php');
// returns: \Users\john\code\app\Models\User
toggle(false);
// returns: true
toggle(true);
// returns: false
generate_password();
// returns: IZeJx3MeUdDhzE2
gettype(auto_cast('42'));
// returns: integer
gettype(auto_cast('42.0'));
// returns: double
gettype(auto_cast('true'));
// returns: boolean
human_filesize(4223);
// returns: 4.12kB
$gen = permutations(['foo', 'bar', 'biz']);
iterator_to_array($gen)
// returns: [
// [
// "foo",
// "bar",
// "biz",
// ],
// [
// "foo",
// "biz",
// "bar",
// ],
// [
// "bar",
// "foo",
// "biz",
// ],
// [
// "bar",
// "biz",
// "foo",
// ],
// [
// "biz",
// "foo",
// "bar",
// ],
// [
// "biz",
// "bar",
// "foo",
// ],
// ]
zenith('civil');
// returns: 96.0
operating_system();
// returns: linux
LINUX
// returns: linux
wikipedia('Towel Day');
// returns: https://en.wikipedia.org/wiki/Towel_Day
wikipedia('Paris', 'fr', '#')
// returns: https://fr.wikipedia.org/wiki/Paris
wikipedia('Pariz', 'fr', '#')
// returns: #
function_location('wikipedia')
// returns: /folder/on/drive/php-helper/src/misc_helper.php:198
function_location('function_does_not_exist')
// returns: null
function_location('array_map')
// returns: '' (empty string)
scrub_url('https://www.repat.de/');
// returns: 'repat.de'
scrub_url('https://blog.fefe.de/?ts=a262bcdf');
// returns: 'blog.fefe.de/?ts=a262bcdf'
http_status_code('httpstat.us/500');
// returns: 500
http_status_code('http://repat.de'); // with 301 redirect to https://repat.de
// returns: 200
http_status_code('http://repat.de', false);
// returns: 301
$requestString = null; // TODO
parse_signed_request($requestString, env('FACEBOOK_CLIENT_SECRET'));
domain_slug('blog.fefe.de')
//returns: blogfefede
domain_slug('blogfefe.de')
//returns: blogfefede
gethostbyname6('ipv4onlydomain.tld');
// returns: ipv4onlydomain.tld
gethostbyname6('example.com')
// returns: 2606:2800:220:1:248:1893:25c8:1946
is_public_ip('127.0.0.1'); // localhost
// returns: false
is_public_ip('::1/128'); // localhost
// returns: false
is_public_ip('192.168.1.42') // private network
// returns: false
$ipv4 = gethostbyname('example.com');
is_public_ip($ipv4);
// returns: true
$ipv6 = gethostbyname6('example.com');
is_public_ip($ipv6);
// returns true;
final_redirect_target('http://google.com');
// returns http://www.google.com
str_icontains('FOOBAR', 'foo');
// returns: true
str_icontains('foobar', 'foo');
// returns: true
str_icontains('foobar', 'FOO');
// returns: true
str_icontains('foobar', 'test');
// returns: false
to_ascii('René');
// returns: Ren
hyphen2_('foo-bar');
// returns: foo_bar
_2hypen('foo_bar');
// returns: foo-bar
str_replace_once('foo', 'bar', 'foofoo');
// returns: 'barfoo'
title_case_wo_underscore('foo_bar');
// returns: Foo Bar
// vs.
// title_case('foo_bar')
// returns: Foo_Bar
lorem_ipsum();
// returns:
// Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
sluggify_domain('blog.fefe.de');
// returns: blog_fefe_de
str_slug('blog.fefe.de');
// returns: blogfefede
sluggify_domain('blogfefe.de');
// returns: blogfefe_de
str_slug('blogfefe.de');
// returns: blogfefede // same as subdomain on fefe.de
str_remove('foobar', 'bar');
// returns: foo
str_remove('foobar42', ['foo', 'bar']);
// returns: 42
str_remove('foobar42', 42);
// returns: foobar
str_bytes('foobar');
// returns: 6
str_bytes('fooßar');
// returns: 8
regex_list(['foo', 'bar', '42'])
// returns: \bfoo|\bbar|\b42
base64_url_decode('aHR0cHM6Ly9yZXBhdC5kZQ==');
// returns: https://repat.de
str_right('https://vimeo.com/165053513', '/');
// returns: 165053513
str_left('https://vimeo.com/165053513', '165053513');
// returns: https://vimeo.com/
normalize_nl('foobar\r\n'); // Windows
// returns: foobar\n
normalize_nl('foobar\r'); // MacOS
// returns: foobar\n
normalize_nl('foobar\n'); // *nix
// returns: foobar\n
str_count_upper('FoObAr');
// returns: 3
str_count_upper('foobar');
// returns: 0
str_count_upper('FOOBAR');
// returns: 6
str_count_lower('FoObAr');
// returns: 3
str_count_lower('foobar');
// returns: 6
str_count_lower('FOOBAR');
// returns: 0
str_insert_bindings('SELECT * FROM `table` WHERE id = ?', [42]);
// returns: SELECT * FROM `table` WHERE id = '42'
contains_uppercase('Foobar');
// returns: true
contains_uppercase('foobar');
// returns: false
contains_uppercase('FOOBAR');
// returns: true
contains_lowercase('Foobar');
// returns: true
contains_lowercase('foobar');
// returns: true
contains_lowercase('FOOBAR');
// returns: false
contains_numbers('Foobar');
// returns: false
contains_numbers('Foobar42');
// returns: true
contains_numbers('42');
// returns: true
contains_numbers(42); // uses strval()
// returns: true
country_name('nz');
// returns: New Zealand
country_name('de');
// returns: Germany (Germany in English)
country_name('de', 'de');
// returns: Deutschland (Germany in German)
remove_accents('á');
// returns: a
remove_accents('René')
// returns: Rene
markdown2html('# Header');
// returns: <h1>Header</h1>\n
// Don't use this code, it's just to illustrate where the file could be
$publicSuffixList = file_get_contents('https://publicsuffix.org/list/public_suffix_list.dat');
$path = '/tmp/public_suffix_list.dat';
file_put_contents($path, $publicSuffixList);
// ...
domain('https://repat.de/about?foo=bar', $path);
// returns: repat.de
linkify('https://google.com is a search engine');
// returns: <a href="https://google.com">google.com</a> is a search engine
linkify('https://google.com is a search engine', ['https'], ['target' => '_blank']);
// returns: <a target="_blank" href="https://google.com">google.com</a> is a search engine
embedded_video_url('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
// returns: https://www.youtube.com/embed/dQw4w9WgXcQ
embedded_video_url('https://vimeo.com/50491748');
// returns: https://player.vimeo.com/video/50491748
ul_li_unpack(['foo' => 'bar']);
// returns: <ul><li>foo: bar</li></ul>
ul_li_unpack(['foo' => 'bar'], '=>');
// returns: <ul><li>foo=> bar</li></ul>
contrast_color('b9b6b6');
// returns: #000000
contrast_color('#496379');
// returns: #ffffff
emoji_flag('nz');
// returns: 🇳🇿
emoji_flag('PY');
// returns: 🇵🇾
emoji_flag(null);
// returns: 🏴