ejz/core

Set of help functions. Added to global scope.

v1.1.6 2017-07-13 20:58 UTC

README

Core is my collections of useful PHP functions. Some functions are just handy wrappers for PHP built-in functions. All functions are added to global scope, no namespaces are required.

Install

$ curl -sS 'https://getcomposer.org/installer' | php
$ php composer.phar require ejz/core:~1.0

To use it, just include vendor/autoload.php in your PHP script.

Requirements

PHP 5.5 or above (with cURL and GD library installed).

Functions

esc

Encode/decode HTML chars in given string: >, < and &. Use this function to escape HTML tags content.

$s = esc("HTML: <>&");
// $s => "HTML: &lt;&gt;&amp;"
$s = esc($s, $decode = true);
// $s => "HTML: <>&"

to top view source

fesc

Encode/decode HTML chars in given string: >, <, &, ' and ". Use this function to escape HTML tags atrribute values.

$s = fesc("HTML: <>&, '\"");
// $s => "HTML: &lt;&gt;&amp;, &#039;&quot;"
$s = esc($s, $decode = true);
// $s => "HTML: <>&, '\""

to top view source

template

Native PHP templating engine.

string template(string $file, array $vars = array());
<!-- test.tpl -->
<html>
<head>
    <title><?=$title?></title>
</head>
<body>
    <?=$body?>
</body>
</html>
echo template("test.tpl", [
    "title" => "Test Title",
    "body" => "<h1>Hello!</h1>",
]);

Output:

<html>
<head>
    <title>Test Title</title>
</head>
<body>
    <h1>Hello!</h1>
</body>
</html>

to top view source

is_host

Validate a hostname (an IP address or domain name).

to top view source

host

Get hostname from URL.

string host(string $url);
$host = host("https://github.com/");
// $host => "github.com"

to top view source

curdate

Get current date in SQL format. Can shift current day using first argument.

string curdate(int $shift_days = 0);
$today = curdate();
// $today => "2017-08-17"
$yesterday = curdate(-1);
// $yesterday => "2017-08-16"

to top view source

now

Get current time is SQL format. Can shift current time using first argument.

string now(int $shift_seconds = 0);
$now = now();
// $now => "2017-08-17 11:04:31"
$min_ago = now(-60);
// $min_ago => "2017-08-17 11:03:31"

to top view source

nsplit

Split line by line given string. Each line is trimmed, empty ones are filtered out.

array nsplit(string $string);

to top view source

is_closure

Return whether or not the provided object is callable.

bool is_closure(object $object);
$bool = is_closure(function() { ; });
// $bool => true

to top view source

is_ip

Whether or not provided IP is valid IP.

bool is_ip(string $ip, bool $allow_private = true);
$ip = "127.0.0.1";
$bool = is_ip($ip);
// $bool => true
$bool = is_ip($ip, $allow_private = false);
// $bool => false

to top view source

is_assoc

Validate associative array.

bool is_assoc(array $array);
$bool = is_assoc([]);
// $bool => true
$bool = is_assoc([1, 2]);
// $bool => false
$bool = is_assoc(["key" => "value"]);
// $bool => true

to top view source

is_regex

Validate regular expression.

bool is_regex(string $regex);
$bool = is_regex("invalid");
// $bool => false
$bool = is_regex("~\w~");
// $bool => true

to top view source

str_replace_once

String replace. Replace is applied only once.

string str_replace_once(string $needle, string $replace, string $haystack);
$str = str_replace_once("foo", "bar", "foo foo");
// $str => "bar foo"

to top view source

str_truncate

Truncate string to certain length (be default 40 chars).

string str_truncate(string $string, int $length = 40, bool $center = false, string $replacer = '...');
$str = str_truncate("Hello, world!", 5);
// $str => "He..."
$str = str_truncate("Hello, world!", 5, $center = true);
// $str => "H...!"

to top view source

mt_shuffle

Shuffle an array using mt_rand(). Can use seed for remembering randomize.

mt_shuffle(array & $array, string|int|null $seed = null);
$arr = ["one", "two", "three"];
mt_shuffle($arr);
// $arr => ["two", "three", "one"]

to top view source

file_get_ext

Get file extension.

string file_get_ext(string $file);
$ext = file_get_ext("image.PNG");
// $ext => "png"
$ext = file_get_ext("archive.tar.gz");
// $ext => "gz"
$ext = file_get_ext("/etc/passwd");
// $ext => ""
$ext = file_get_ext("/var/www/");
// $ext => ""

to top view source

file_get_name

Get file name (without extension).

string file_get_name(string $file);
$name = file_get_name("image.png");
// $name => "image"
$name = file_get_name("archive.tar.gz");
// $name => "archive.tar"
$name = file_get_name("/etc/passwd");
// $name => "passwd"
$name = file_get_name("/var/www/");
// $name => ""

to top view source

rand_from_string

Get random integer from string.

int rand_from_string(string $string);
$int = rand_from_string("one");
// $int => 48928
$int = rand_from_string("two");
// $int => 48928
$int = rand_from_string("one");
// $int => 48928

to top view source

get_user_agent

Randomly get User agent string.

string get_user_agent(string|null $filter = null, string|int|null $seed = null);
$ua = get_user_agent();
// $ua => "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)"
$ua = get_user_agent("invalid filter");
// $ua => ""
$ua = get_user_agent("opera");
// $ua => "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; de) Opera 11.51"
var_dump(get_user_agent("Mac OS X", "seed") === get_user_agent("Mac OS X", "seed"));
// => bool(true)

to top view source

get_tag_attributes

Get tag attributes. Returns list. If second argument is not null, returns value of this argument (or null if no such argument).

array|string|null get_user_agent(string|DOMNode $tag, string|null $attr = null);
$tag = "<a href='/link.html?a=1&amp;b=2'>";
$attrs = get_tag_attributes($tag);
// $attrs => ["href" => "/link.html?a=1&b=2"]
$attr = get_tag_attributes($tag, 'href');
// $attr => "/link.html?a=1&b=2"
$attr = get_tag_attributes($tag, '_target');
// $attr => null

to top view source

prepare_tag_attributes

Prepare attributes for outputing in HTML tag.

string prepare_tag_attributes(array $attributes);
$attributes = ["href" => "/link.html?a=1&b=2", "class" => ["_left", "_clearfix"]];
$prepared = prepare_tag_attributes($attributes);
// $prepared => "href=\"/link.html?a=1&amp;b=2\" class=\"_left _clearfix\""
$attributes = ["style" => ["margin-top" => "0", "display" => "flex"]];
$prepared = prepare_tag_attributes($attributes);
// $prepared => "style=\"margin-top:0;display:flex;\""

to top view source

realurl

Get absolute URL, also lead URL to more canonical form.

string|null realurl(string $url, string $absolute = '');
$url = realurl("/link.html", "http://site.com/");
// $url => "http://site.com/link.html"
$url = realurl("http://site.com/archive/2014/../link.html");
// $url => "http://site.com/archive/link.html"
$url = realurl("../home.html", "http://site.com/archive/link.html");
// $url => "http://site.com/home.html"

to top view source

xpath

Wrapper around DOMXPath. Accepts XPath queries for extracting tags and callback function for tag manipulating.

array|string xpath(string|DOMNode $xml, string $query = '/*', callable|int|null $callback = null, array $flags = []);
$content = file_get_contents("http://github.com/");
$metas = xpath($content, "//meta");
print_r($metas);

Output:

Array
(
    [0] => <meta charset="utf-8"/>
    [1] => <meta name="viewport" content="width=device-width"/>
    [2] => <meta property="og:url" content="https://github.com"/>
    [3] => <meta name="pjax-timeout" content="1000"/>
    [4] => <meta name="theme-color" content="#1e2327"/>
)

For more examples, please, refer to xpath.md.

to top view source

_expect

Raise an error, if given variable does not match type.

_expect(mixed $var, string $types);
$a = 'string';
_expect($a, 'string|null');

to top view source

_log

Raise a user defined error with a message. Shortcut to trigger_error() function. For CLI mode outputs to STDERR instead of raising an error. If E_USER_ERROR is raised, exit with error code 1.

_log(string $msg, int $level = E_USER_NOTICE);

to top view source

_warn

Raise E_USER_WARNING with a message.

_warn(string $msg);

to top view source

_err

Raise E_USER_ERROR with a message. Exit with error code 1.

_err(string $msg);

to top view source

Authors

License

Core is licensed under the WTFPL License (see LICENSE).