Files
core/e_lib/utilphp-1.1.0/docs/index.html
T
Hide_D 7e0940d57d ?> 제거
replace 코드 디버깅 용도로 제거
2018-01-14 02:31:25 +09:00

1235 lines
56 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="chrome=1" />
<meta name="viewport" content="width=device-width" />
<link rel="canonical" href="http://includephp.brandonwamboldt.ca/" />
<link rel="shortcut icon" href="favicon.png" type="image/png" />
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic" />
<link rel="stylesheet" href="css/docs.css" />
<title>util.php - The PHP programmers best friend - UtilityPHP</title>
</head>
<body>
<div id="sidebar" class="interface">
<a class="toc_title" href="#">util.php <span class="version">(1.1.0)</span></a>
<a class="toc_title" href="#debug_functions">Debugging</a>
<ul class="toc_section">
<li>- <a href="#var_dump">var_dump</a></li>
</ul>
<a class="toc_title" href="#arrays">Arrays &amp; Objects</a>
<ul class="toc_section">
<li>- <a href="#array_first">array_first</a></li>
<li>- <a href="#array_first_key">array_first_key</a></li>
<li>- <a href="#array_flatten">array_flatten</a></li>
<li>- <a href="#array_get">array_get</a></li>
<li>- <a href="#array_last">array_last</a></li>
<li>- <a href="#array_last_key">array_last_key</a></li>
<li>- <a href="#array_map_deep">array_map_deep</a></li>
<li>- <a href="#array_pluck">array_pluck</a></li>
<li>- <a href="#array_search_deep">array_search_deep</a></li>
<li>- <a href="#array_clean">array_clean</a></li>
</ul>
<a class="toc_title" href="#urls">URLs</a>
<ul class="toc_section">
<li>- <a href="#add_query_arg">add_query_arg</a></li>
<li>- <a href="#get_current_url">get_current_url</a></li>
<li>- <a href="#http_build_url">http_build_url</a></li>
<li>- <a href="#is_https">is_https</a></li>
<li>- <a href="#remove_query_arg">remove_query_arg</a></li>
<li>- <a href="#slugify">slugify</a></li>
</ul>
<a class="toc_title" href="#strings">Strings</a>
<ul class="toc_section">
<li>- <a href="#htmlentities">htmlentities</a></li>
<li>- <a href="#htmlspecialchars">htmlspecialchars</a></li>
<li>- <a href="#linkify">linkify</a></li>
<li>- <a href="#match_string">match_string</a></li>
<li>- <a href="#random_string">random_string</a></li>
<li>- <a href="#number_to_word">number_to_word</a></li>
<li>- <a href="#ordinal">ordinal</a></li>
<li>- <a href="#remove_accents">remove_accents</a></li>
<li>- <a href="#secure_random_string">secure_random_string</a></li>
<li>- <a href="#seems_utf8">seems_utf8</a></li>
<li>- <a href="#safe_truncate">safe_truncate</a></li>
<li>- <a href="#size_format">size_format</a></li>
<li>- <a href="#str_to_bool">str_to_bool</a></li>
<li>- <a href="#zero_pad">zero_pad</a></li>
<li>- <a href="#strip_space">strip_space</a></li>
<li>- <a href="#sanitize_string">sanitize_string</a></li>
</ul>
<a class="toc_title" href="#files">Files</a>
<ul class="toc_section">
<li>- <a href="#full_permissions">full_permissions</a></li>
<li>- <a href="#rmdir">rmdir</a></li>
</ul>
<a class="toc_title" href="#serialization">Serialization</a>
<ul class="toc_section">
<li>- <a href="#is_serialized">is_serialized</a></li>
<li>- <a href="#maybe_serialize">maybe_serialize</a></li>
<li>- <a href="#maybe_unserialize">maybe_unserialize</a></li>
<li>- <a href="#fix_broken_serialization">fix_broken_serialization</a></li>
</ul>
<a class="toc_title" href="#other">Other</a>
<ul class="toc_section">
<li>- <a href="#force_download">force_download</a></li>
<li>- <a href="#get_client_ip">get_client_ip</a></li>
<li>- <a href="#get_gravatar">get_gravatar</a></li>
<li>- <a href="#human_time_diff">human_time_diff</a></li>
<li>- <a href="#nocache_headers">nocache_headers</a></li>
<li>- <a href="#utf8_headers">utf8_headers</a></li>
<li>- <a href="#validate_email">validate_email</a></li>
</ul>
<a class="toc_title" href="#constants">Constants</a>
<ul class="toc_section">
<li>- <a href="#SECONDS_IN_A_MINUTE">SECONDS_IN_A_MINUTE</a></li>
<li>- <a href="#SECONDS_IN_AN_HOUR">SECONDS_IN_AN_HOUR</a></li>
<li>- <a href="#SECONDS_IN_A_DAY">SECONDS_IN_A_DAY</a></li>
<li>- <a href="#SECONDS_IN_A_WEEK">SECONDS_IN_A_WEEK</a></li>
<li>- <a href="#SECONDS_IN_A_MONTH">SECONDS_IN_A_MONTH</a></li>
<li>- <a href="#SECONDS_IN_A_YEAR">SECONDS_IN_A_YEAR</a></li>
</ul>
<a class="toc_title" href="#changelog">Change Log</a>
</div>
<div class="container">
<p id="introduction">
<img id="logo" src="images/utility_logo.png" alt="Util.php" />
</p>
<p>
<a href="http://github.com/brandonwamboldt/utilphp/">util.php</a> is a collection of useful functions and snippets
that you need or could use every day. It's implemented as a class with static methods, to avoid conflicts with your
existing code-base. Just drop it in and start using it immediately.
</p>
<p>
Included are 60+ functions that provide you with the ability to do common tasks much easier and more efficiently,
without having to find that one comment on php.net where you know its been done already. Access superglobals
without checking to see if certain indexes are set first and pass default values, use a nicely formatted var dump,
validate emails, generate random strings, flatten an array, pull a single column out of a multidimensional array
and much more.
</p>
<p>
Although it's implemented as one giant class, util.php has extensive documentation and a full suite of unit tests
to avoid breaking backwards-compatibility unintentionally.
</p>
<p>
The project is <a href="http://github.com/brandonwamboldt/utilphp/">hosted on GitHub</a>.
You can report bugs and discuss features on the
<a href="https://github.com/brandonwamboldt/utilphp/issues">issues page</a>,
or send tweets to <a href="http://twitter.com/brandonwamboldt">@brandonwamboldt</a>.
</p>
<h2>Downloads <i style="padding-left: 12px; font-size:12px;">(Right-click, and use "Save As")</i></h2>
<p>
<a href="https://github.com/brandonwamboldt/utilphp/archive/1.1.0.zip">Stable Version (1.1.0)</a><br>
<a href="https://github.com/brandonwamboldt/utilphp/zipball/master">Unstable Version (master)</a>
</p>
<h2>Installation</h2>
<span class="header">Server Requirements</span><br>
<ul>
<li>PHP 5.3.3+</li>
</ul>
<span class="header">Standalone File</span><br>
<p class="desc-text">
Simply drop <code>util.php</code> in any project and call <code>include 'util.php';</code> in your project.
You can then access the <code>Util</code> class.
</p>
<span class="header">Composer</span><br>
<p class="desc-text">
Add the following dependency to your composer.json:
<pre>"brandonwamboldt/utilphp": "1.1.*"</pre>
When used with composer, the class is namespaced (<code>\utilphp\util</code>) instead of
just <code>util</code>.
</p>
<div id="documentation">
<h2 id="debug_functions">Debugging Functions</h2>
<section>
<article id="var_dump">
<p>
<span class="header">var_dump</span>
<code>
<span class="util_class">util::</span><span class="func_name">var_dump</span>(mixed <span class="param">$var</span>[, bool <span class="param">$return</span> = FALSE])
</code>
</p>
<p>
Nicely prints out the contents of a passed variable. Booleans are properly displayed as 'true' or 'false',
null values are shown as 'NULL', variable types are displayed, and arrays and objects are displayed as a
collapsible tree powered by JavaScript.
</p>
<p><strong>Usage:</strong></p>
<pre class="usage-example">util::var_dump($var);</pre>
<img src="images/awesome_var_dump.jpg" style="border: 1px solid #ccc;" />
</article>
</section>
<h2 id="arrays">Array/Object Functions</h2>
<section>
<article id="array_first">
<p>
<span class="header">array_first</span>
<code>
<span class="util_class">util::</span><span class="func_name">array_first</span>(array <span class="param">$array</span>)
</code>
</p>
<p>
Retrieve the first value from an array. Can be run on function callbacks, and it will not modify the pointer
of the source array unlike most other methods of doing this.
</p>
<p><strong>Usage:</strong></p>
<pre>util::array_first( ['a', 'b', 'c'] );<br />=&gt; Returns 'a'</pre>
</article>
<article id="array_first_key">
<p>
<span class="header">array_first_key</span>
<code>
<span class="util_class">util::</span><span class="func_name">array_first_key</span>(array <span class="param">$array</span>)
</code>
</p>
<p>
Retrieve the first key from an array. Can be run on function callbacks, and it will not modify the pointer of
the source array unlike most other methods of doing this.
</p>
<p><strong>Usage:</strong></p>
<pre>util::array_first_key( $users );<br />=&gt; Returns 'brandon'</pre>
</article>
<article id="array_flatten">
<p>
<span class="header">array_flatten</span>
<code>
<span class="util_class">util::</span><span class="func_name">array_flatten</span>(array <span class="param">$array</span>[, bool <span class="param">$preserve_keys</span> = TRUE])
</code>
</p>
<p>Flattens a multi-dimensional array into a one dimensional array</p>
<p><strong>Usage:</strong></p>
<pre>util::array_flatten( [ 'a', 'b', [ 'c', 'd', 'e', [ 'f', 'g', [ [ [ [ 'h', 'i', 'j' ] ] ] ] ] ] ] );
=&gt; Returns [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' ]</pre>
</article>
<article id="array_get">
<p>
<span class="header">array_get</span>
<code>
<span class="util_class">util::</span><span class="func_name">array_get</span>(<span class="param">$var</span>[, mixed <span class="param">$default</span> = NULL])
</code>
</p>
<p>Retrieve a value from an array, or return a given default if the index isn't set</p>
<p><strong>Usage:</strong></p>
<pre>util::array_get($_POST['action'], 'index');
=&gt; If $_POST['action'] is set, returns its value, otherwise returns 'index'
util::array_get($_POST['action']['do'], 'index');
=&gt; Returns the value of $_POST['action']['do'] or 'index'</pre>
</article>
<article id="array_last">
<p>
<span class="header">array_last</span>
<code>
<span class="util_class">util::</span><span class="func_name">array_last</span>(array <span class="param">$array</span>)
</code>
</p>
<p>
Retrieve the last value from an array. Can be run on function callbacks, and it will not modify the pointer
of the source array unlike most other methods of doing this.
</p>
<p><strong>Usage:</strong></p>
<pre>util::array_last( [ 'a', 'b', 'c'] );
=&gt; Returns 'c'</pre>
</article>
<article id="array_last_key">
<p>
<span class="header">array_last_key</span>
<code>
<span class="util_class">util::</span><span class="func_name">array_last_key</span>(array <span class="param">$array</span>)
</code>
</p>
<p>
Retrieve the last key from an array. Can be run on function callbacks, and it will not modify the pointer of
the source array unlike most other methods of doing this.
</p>
<p><strong>Usage:</strong></p>
<pre>util::array_last_key( $users );
=&gt; Returns 'zane'</pre>
</article>
<article id="array_map_deep">
<p>
<span class="header">array_map_deep</span>
<code>
<span class="util_class">util::</span><span class="func_name">array_map_deep</span>(array <span class="param">$array</span>, callable <span class="param">$callback</span>[, bool <span class="param">$on_nonscalar</span> = FALSE])
</code>
</p>
<p>
Returns an array containing all the elements of $array after applying the callback function to each one recursively.
Particularly useful for avoiding errors when calling functions that only accept scalar values on an array that could
contain nested arrays, such as $_GET or $_POST.
</p>
<p><strong>Usage:</strong></p>
<pre>util::array_map_deep($_POST, 'htmlentities');
=&gt; Recursively escapes each scalar value in $_POST
util::array_map_deep($_POST, 'htmlentities', TRUE);
=&gt; Recursively escapes each value in $_POST regardless of its type</pre>
</article>
<article id="array_pluck">
<p>
<span class="header">array_pluck</span>
<code>
<span class="util_class">util::</span><span class="func_name">array_pluck</span>(array <span class="param">$array</span>, string <span class="param">$field</span>[, bool <span class="param">$preserve_keys</span>, bool <span class="param">$remove_nomatches</span>])
</code>
</p>
<p>
Replaces each value in an array with the specified field of the array or object that used to be the value. Very
useful when you have an array of objects or arrays from a database, and you only want one specific field. For
example, you want an array of emails from an array of users.
</p>
<p><strong>Usage:</strong></p>
<pre>util::array_pluck([['val' => 1], ['val' => 2], ['val' => 3]], 'val');
=&gt; Returns [1, 2, 3]
util::array_pluck($users, 'email');
=&gt; Returns an array of email addresses for each user</pre>
</article>
<article id="array_search_deep">
<p>
<span class="header">array_search_deep</span>
<code>
<span class="util_class">util::</span><span class="func_name">array_search_deep</span>(array <span class="param">$array</span>, mixed <span class="param">$search</span>[, string <span class="param">$field</span> = FALSE])
</code>
</p>
<p>
Allows you to search for a value in an array of arrays or an array of objects, and return the key of the value
that matches the search criteria. If $field is unspecified, the entire array/object is searched.
</p>
<p><strong>Usage:</strong></p>
<pre>util::array_search_deep($_POST, 'delete_post');
=&gt; Might return 'do' if $_POST['do']['action'] = 'delete_post'
util::array_search_deep($users, 'rogue_coder', 'username');
=&gt; Might return 5 if $users[5]->username = 'rogue_coder'</pre>
</article>
<article id="array_clean">
<p>
<span class="header">array_clean</span>
<code>
<span class="util_class">util::</span><span class="func_name">array_clean</span>(array <span class="param">$array</span>)
</code>
</p>
<p>
Remove all null/empty/false values from the given array.
</p>
<p><strong>Usage:</strong></p>
<pre>util::array_clean( array( 'a', 'b', '', null, false, 0) );<br />=&gt; Returns array('a', 'b');</pre>
</article>
</section>
<h2 id="urls">URL Functions</h2>
<section>
<article id="add_query_arg">
<p>
<span class="header">add_query_arg</span>
<code>
<span class="util_class">util::</span><span class="func_name">add_query_arg</span>()
</code>
</p>
<p>Adds one ore more query args to the query string of the current URL or a given URL</p>
<p><strong>Usage:</strong></p>
<pre>util::add_query_arg( 'user', '5', '/admin/users?action=edit' );
=&gt; Returns '/admin/users?action=edit&amp;user=5'
util::add_query_arg( [ 'user' => 5, 'action' => 'edit' ], '/admin/users' );
=&gt; Returns '/admin/users?user=5&amp;action=edit'</pre>
</article>
<article id="get_current_url">
<p>
<span class="header">get_current_url</span>
<code>
<span class="util_class">util::</span><span class="func_name">get_current_url</span>()
</code>
</p>
<p>Returns the current URL with hostname</p>
<p><strong>Usage:</strong></p>
<pre>util::get_current_url();
=&gt; Returns 'http://brandonwamboldt.ca/utilphp/'</pre>
</article>
<article id="is_https">
<p>
<span class="header">is_https</span>
<code>
<span class="util_class">util::</span><span class="func_name">is_https</span>()
</code>
</p>
<p>Returns true if the current page is being loaded over https, false if it isn't</p>
<p><strong>Usage:</strong></p>
<pre>util::is_https();
=&gt; Returns false</pre>
</article>
<article id="http_build_url">
<p>
<span class="header">http_build_url</span>
<code>
<span class="util_class">util::</span><span class="func_name">http_build_url</span>([mixed $url[, mixed $parts[, int $flags = HTTP_URL_REPLACE[, array &amo;$new_url]]]])
</code>
</p>
<p>
Pure PHP implementation/polyfill for <a href="http://php.net/http_build_url">http_build_url</a>
which requires pecl_http to be installed.
</p>
<p><strong>Usage:</strong></p>
<pre>echo util::http_build_url("http://user@www.example.com/pub/index.php?a=b#files",
array(
"scheme" => "ftp",
"host" => "ftp.example.com",
"path" => "files/current/",
"query" => "a=c"
),
HTTP_URL_STRIP_AUTH | HTTP_URL_JOIN_PATH | HTTP_URL_JOIN_QUERY | HTTP_URL_STRIP_FRAGMENT
);</pre>
<p>The above example will output:</p>
<pre>ftp://ftp.example.com/pub/files/current/?a=c</pre>
</article>
<article id="is_https">
<p>
<span class="header">is_https</span>
<code>
<span class="util_class">util::</span><span class="func_name">is_https</span>()
</code>
</p>
<p>Returns true if the current page is being loaded over https, false if it isn't</p>
<p><strong>Usage:</strong></p>
<pre>util::is_https();
=&gt; Returns false</pre>
</article>
<article id="remove_query_arg">
<p>
<span class="header">remove_query_arg</span>
<code>
<span class="util_class">util::</span><span class="func_name">remove_query_arg</span>( array|string <span class="param">$query_args</span>[, string <span class="param">$url</span>])
</code>
</p>
<p>Removes one ore more query args from the query string of the current URL or a given URL</p>
<p><strong>Usage:</strong></p>
<pre>util::remove_query_arg( 'action', '/admin/users?action=edit&amp;user=5' );
=&gt; Returns '/admin/users?user=5'
util::remove_query_arg( [ 'user', 'action'], '/admin/users?action=edit&amp;user=5' );
=&gt; Returns '/admin/users'</pre>
</article>
<article id="slugify">
<p>
<span class="header">slugify</span>
<code>
<span class="util_class">util::</span><span class="func_name">slugify</span>(string <span class="param">$string</span>[, string <span class="param">$separator</span> = "-", bool <span class="param">$css_mode</span> = FALSE])
</code>
</p>
<p>
Generate a string safe for use in URLs from any given string. Converts any accent characters
to their equivalent normal characters and converts any other non-alphanumeric characters to
dashes, then converts any sequence of two or more dashes to a single dash. This function
generates slugs safe for use as URLs, and if you pass TRUE as the second parameter, it will
create strings safe for use as CSS classes or IDs.
</p>
<p><strong>Usage:</strong></p>
<pre>util::slugify('This is a random --string with an Ãccent');
=&gt; Returns 'this-is-a-random-string-with-an-accent'
util::slugify('Another String', '.');
=&gt; Returns 'another.string'</pre>
</article>
</section>
<h2 id="strings">String Functions</h2>
<section>
<article id="htmlentities">
<p>
<span class="header">htmlentities</span>
<code>
<span class="util_class">util::</span><span class="func_name">htmlentities</span>(string <span class="param">$string</span>[, bool <span class="param">$preserve_encoded_entities</span> = FALSE])
</code>
</p>
<p>
Executes htmlentities with ENT_QUOTES set by default. However, if you pass TRUE as
the second parameter, it will not re-encode entities that are already encoded.
</p>
<p><strong>Usage:</strong></p>
<pre>util::htmlentities('this string &lt; this string', TRUE);
=&gt; Returns 'this string &lt; this string'</pre>
</article>
<article id="htmlspecialchars">
<p>
<span class="header">htmlspecialchars</span>
<code>
<span class="util_class">util::</span><span class="func_name">htmlspecialchars</span>(string <span class="param">$string</span>[, bool <span class="param">$preserve_encoded_entities</span> = FALSE])
</code>
</p>
<p>
Executes htmlspecialchars with ENT_QUOTES set by default. However, if you pass TRUE as
the second parameter, it will not re-encode entities that are already encoded.
</p>
<p><strong>Usage:</strong></p>
<pre>util::htmlspecialchars('this string &lt; this string', TRUE);
=&gt; Returns 'this string &lt; this string'</pre>
</article>
<article id="linkify">
<p>
<span class="header">linkify</span>
<code>
<span class="util_class">util::</span><span class="func_name">linkify</span>(string <span class="param">$text</span>)
</code>
</p>
<p>Find's any URLs in the specified text and wraps them with HTML anchor tags</p>
<p><strong>Usage:</strong></p>
<pre>util::linkify('this string has a link to www.google.com');
=&gt; Returns 'this string has a link to &lt;a href="http://wwww.google.com/"&gt;www.google.com&lt;/a&gt;'</pre>
</article>
<article id="match_string">
<p>
<span class="header">match_string</span>
<code>
<span class="util_class">util::</span><span class="func_name">match_string</span>(string <span class="param">$pattern</span>, string <span class="param">$string</span>[, bool <span class="param">$caseSensitive</span> = true])
</code>
</p>
<p>
Check if a string matches a given pattern. You can use '*' as a wildcard character.
</p>
<p><strong>Usage:</strong></p>
<pre>util::match_string("test/*", "test/my/test");
=&gt; Returns true
util::match_string("test/", "test/my/test");
=&gt; Returns false</pre>
</article>
<article id="random_string">
<p>
<span class="header">random_string</span>
<code>
<span class="util_class">util::</span><span class="func_name">random_string</span>(int <span class="param">$length</span>[, bool <span class="param">$human_friendly</span> = TRUE, bool <span class="param">$include_symbols</span> = FALSE, bool <span class="param">$no_duplicate_chars</span> = FALSE])
</code>
</p>
<p>
Generates a random string of the specified length. Human friendly mode
will exclude characters that can be confused with other characters, such
as 0 (zero) and O (capital o), 1 (one) and l (lowercase L).
</p>
<p><strong>Usage:</strong></p>
<pre>util::random_string(8);
=&gt; Returns 'a6BiF4UW'</pre>
</article>
<article id="number_to_word">
<p>
<span class="header">number_to_word</span>
<code>
<span class="util_class">util::</span><span class="func_name">number_to_word</span>(int|float <span class="param">$number</span>)
</code>
</p>
<p>Converts a number into its text equivalent. For example, 56 becomes fifty-six.</p>
<pre>util::number_to_word(512.5);
=&gt; Returns 'five hundred and twelve point five'</pre>
</article>
<article id="ordinal">
<p>
<span class="header">ordinal</span>
<code>
<span class="util_class">util::</span><span class="func_name">ordinal</span>(int <span class="param">$number</span>)
</code>
</p>
<p>Returns the ordinal version of a number (appends th, st, nd, rd)</p>
<p><strong>Usage:</strong></p>
<pre>util::ordinal(22);
=&gt; Returns '22nd'</pre>
</article>
<article id="remove_accents">
<p>
<span class="header">remove_accents</span>
<code>
<span class="util_class">util::</span><span class="func_name">remove_accents</span>(string <span class="param">$string</span>)
</code>
</p>
<p>Converts all accent characters to their ASCII equivalents</p>
<p><strong>Usage:</strong></p>
<pre>util::remove_accents('&#192;ccent');
=&gt; Returns 'Accent'</pre>
</article>
<article id="secure_random_string">
<p>
<span class="header">secure_random_string</span>
<code>
<span class="util_class">util::</span><span class="func_name">secure_random_string</span>(int <span class="param">$length</span>)
</code>
</p>
<p>Generate a random string of characters. Will attempt to use a secure source (openssl).</p>
<p><strong>Usage:</strong></p>
<pre>util::secure_random_string(16);
=&gt; Returns '5bB1RJH0cQhNjviT'</pre>
</article>
<article id="seems_utf8">
<p>
<span class="header">seems_utf8</span>
<code>
<span class="util_class">util::</span><span class="func_name">seems_utf8</span>(string <span class="param">$string</span>)
</code>
</p>
<p>Checks to see if a given string is UTF-8 encoded/safe</p>
<p><strong>Usage:</strong></p>
<pre>util::seems_utf8('This is some random string user input');
=&gt; Returns true</pre>
</article>
<article id="safe_truncate">
<p>
<span class="header">safe_truncate</span>
<code>
<span class="util_class">util::</span><span class="func_name">safe_truncate</span>(string <span class="param">$string</span>, int <span class="param">$length</span>[, string <span class="param">$append</span> = '...'])
</code>
</p>
<p>Truncate a string to a specified length without cutting a word off</p>
<p><strong>Usage:</strong></p>
<pre>util::safe_truncate('The quick brown fox jumps over the lazy dog', 24);
=&gt; Returns 'The quick brown fox...'</pre>
</article>
<article id="size_format">
<p>
<span class="header">size_format</span>
<code>
<span class="util_class">util::</span><span class="func_name">size_format</span>(int <span class="param">$bytes</span>[, int <span class="param">$decimals</span> = 0])
</code>
</p>
<p>Formats an integer as a human friendly size string, such as 4 KiB.</p>
<p><strong>Usage:</strong></p>
<pre>util::size_format( 25151251, 2 );
=&gt; Returns '23.99 MiB'</pre>
</article>
<article id="str_to_bool">
<p>
<span class="header">str_to_bool</span>
<code>
<span class="util_class">util::</span><span class="func_name">str_to_bool</span>(string <span class="param">$string</span>)
</code>
</p>
<p>Converts a string to boolean, looking for yes/no words like 'yes', 'no', 'true', 'false', 'y', 'n', etc</p>
<p><strong>Usage:</strong></p>
<pre>util::str_to_bool('yes');
=&gt; Returns true</pre>
</article>
<article id="zero_pad">
<p>
<span class="header">zero_pad</span>
<code>
<span class="util_class">util::</span><span class="func_name">zero_pad</span>(int <span class="param">$number</span>, int <span class="param">$length</span>)
</code>
</p>
<p>Pads a given number with zeroes on the left</p>
<p><strong>Usage:</strong></p>
<pre>util::zero_pad(341, 8);
=&gt; Returns '00000341'</pre>
</article>
<article id="strip_space">
<p>
<span class="header">strip_space</span>
<code>
<span class="util_class">util::</span><span class="func_name">strip_space</span>(string <span class="param">$string</span>)
</code>
</p>
<p>Strip all withspace from the given string</p>
<p><strong>Usage:</strong></p>
<pre>util::strip_space(' The quick brown fox jumps over the lazy dog ');
=&gt; Returns 'Thequickbrownfoxjumpsoverthelazydog'</pre>
</article>
<article id="sanitize_string">
<p>
<span class="header">sanitize_string</span>
<code>
<span class="util_class">util::</span><span class="func_name">sanitize_string</span>(string <span class="param">$string</span>)
</code>
</p>
<p>
Sanitize a string by performing the following operation :
<ul>
<li>Remove accents</li>
<li>Lower the string</li>
<li>Remove punctuation characters</li>
<li>Strip whitespaces</li>
</ul>
</p>
<p><strong>Usage:</strong></p>
<pre>util::sanitize_string(' Benoit! à New-York? jai perçu 1 % : Quas-tu "gagné" chez M. V. Noël? Dix francs.');
=&gt; Returns 'benoitanewyorkjaipercu1quastugagnechezmvnoeldixfrancs'</pre>
</article>
</section>
<h2 id="files">Files</h2>
<section>
<article id="full_permissions">
<p>
<span class="header">full_permissions</span>
<code>
<span class="util_class">util::</span><span class="func_name">full_permissions</span>(string <span class="param">$file</span>)
</code>
</p>
<p>Returns the file permissions as a nice string, like -rw-r--r--</p>
<p><strong>Usage:</strong></p>
<pre>util::full_permissions('/etc/passwd');
=&gt; Returns '-rw-r--r--'</pre>
</article>
<article id="rmdir">
<p>
<span class="header">rmdir</span>
<code>
<span class="util_class">util::</span><span class="func_name">rmdir</span>(string <span class="param">$dir</span>[, bool <span class="param">$traverseSymlinks</span> = false])
</code>
</p>
<p>Recursively deletes a directories (including all contents).</p>
<p><strong>Usage:</strong></p>
<pre>util::rmdir('/tmp/working/dir');
=&gt; Returns true</pre>
</article>
</section>
<h2 id="serialization">Serialization</h2>
<section>
<article id="is_serialized">
<p>
<span class="header">is_serialized</span>
<code>
<span class="util_class">util::</span><span class="func_name">is_serialized</span>(mixed <span class="param">$data</span>)
</code>
</p>
<p>Checks to see if a given variable is a seralized data structure</p>
<p><strong>Usage:</strong></p>
<pre>util::is_serialized( 'a:0:{}' );
=&gt; Returns true</pre>
</article>
<article id="maybe_serialize">
<p>
<span class="header">maybe_serialize</span>
<code>
<span class="util_class">util::</span><span class="func_name">maybe_serialize</span>(mixed <span class="param">$data</span>)
</code>
</p>
<p>Serialize data, if needed to store in plaintext (Arrays or objects)</p>
<p><strong>Usage:</strong></p>
<pre>util::maybe_serialize( 5 );
=&gt; Returns 5
util::maybe_serialize( array() );
=&gt; Returns 'a:0:{}'</pre>
</article>
<article id="maybe_unserialize">
<p>
<span class="header">maybe_unserialize</span>
<code>
<span class="util_class">util::</span><span class="func_name">maybe_unserialize</span>(mixed <span class="param">$data</span>)
</code>
</p>
<p>Unserialize data, if it's a serialized string</p>
<p><strong>Usage:</strong></p>
<pre>util::maybe_unserialize(5);
=&gt; Returns 5
util::maybe_unserialize("a:0:{}");
=&gt; Returns array()</pre>
</article>
<article id="fix_broken_serialization">
<p>
<span class="header">fix_broken_serialization</span>
<code>
<span class="util_class">util::</span><span class="func_name">fix_broken_serialization</span>(mixed <span class="param">$data</span>)
</code>
</p>
<p>
Attempt to fix a broken serialization string (e.g. where string offsets are incorrect). Can fix
errors that frequently occur with mismatched character sets or higher-than-ASCII characters.
</p>
<p><strong>Usage:</strong></p>
<pre>util::fix_broken_serialization('a:1:{s:4:"test";s:4:"abc";}');
=&gt; Returns 'a:1:{s:4:"test";s:3:"abc";}'</pre>
</article>
</section>
<h2 id="other">Other</h2>
<section>
<article id="force_download">
<p>
<span class="header">force_download</span>
<code>
<span class="util_class">util::</span><span class="func_name">force_download</span>(string <span class="param">$filename</span>, string <span class="param">$content</span> = FALSE)
</code>
</p>
<p>
Transmit headers that force a browser to display the download file dialog. Cross
browser compatible. Only fires if headers have not already been sent.
</p>
<p><strong>Usage:</strong></p>
<pre>util::force_download( 'export.csv', file_get_contents( 'securefile.csv' ) );
=&gt; The user will be prompted to download the file</pre>
</article>
<article id="get_client_ip">
<p>
<span class="header">get_client_ip</span>
<code>
<span class="util_class">util::</span><span class="func_name">get_client_ip</span>(bool <span class="param">$trust_proxy_headers</span> = FALSE)
</code>
</p>
<p>
Returns the IP address of the client. Only enable $trust_proxy_headers if your server is behind a proxy
that sets the HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR headers.
</p>
<p><strong>Usage:</strong></p>
<pre>util::get_client_ip();
=&gt; Returns '12.123.12.100'</pre>
</article>
<article id="get_gravatar">
<p>
<span class="header">get_gravatar</span>
<code>
<span class="util_class">util::</span><span class="func_name">get_gravatar</span>(string <span class="param">$email</span>, int <span class="param">$size</span> = 32)
</code>
</p>
<p>Return the URL to a user's gravatar</p>
<p><strong>Usage:</strong></p>
<pre>util::get_gravatar('brandon.wamboldt@gmail.com');
=&gt; Returns 'http://www.gravatar.com/avatar/46679faeb6780ecb1ea57527fdc66eb3?s=32'</pre>
</article>
<article id="human_time_diff">
<p>
<span class="header">human_time_diff</span>
<code>
<span class="util_class">util::</span><span class="func_name">human_time_diff</span>(int <span class="param">$from</span>, int <span class="param">$to</span> = '', int <span class="param">$as_text</span> = FALSE, string <span class="param">$suffix</span> = ' ago')
</code>
</p>
<p>Converts a unix timestamp to a relative time string, such as "3 days ago" or "2 weeks ago"</p>
<p><strong>Usage:</strong></p>
<pre>util::human_time_diff(time() - 7400);
=&gt; Returns '2 hours ago'
util::human_time_diff(time() - 7400, '', TRUE);
=&gt; Returns 'two hours ago'</pre>
</article>
<article id="nocache_headers">
<p>
<span class="header">nocache_headers</span>
<code>
<span class="util_class">util::</span><span class="func_name">nocache_headers</span>(mixed <span class="param">$data</span>)
</code>
</p>
<p>Sets the headers to prevent caching for the different browsers</p>
<p>
Different browsers support different nocache headers, so several headers must be sent
so that all of them get the point that no caching should occur
</p>
<p><strong>Usage:</strong></p>
<pre>util::nocache_headers();</pre>
</article>
<article id="utf8_headers">
<p>
<span class="header">utf8_headers</span>
<code>
<span class="util_class">util::</span><span class="func_name">utf8_headers</span>(mixed <span class="param">$content_type</span> = 'text/html')
</code>
</p>
<p>Transmit UTF-8 content headers if the headers haven't already been sent</p>
<p><strong>Usage:</strong></p>
<pre>util::utf8_headers();</pre>
</article>
<article id="validate_email">
<p>
<span class="header">validate_email</span>
<code>
<span class="util_class">util::</span><span class="func_name">validate_email</span>(string <span class="param">$possible_email</span>)
</code>
</p>
<p>Validate an email address</p>
<p><strong>Usage:</strong></p>
<pre>util::validate_email( 'brandon.wamboldt@gmail.com' );
=&gt; Returns true
util::validate_email( 'not an email' );
=&gt; Returns false</pre>
</article>
</section>
<h2 id="constants">Constants</h2>
<section>
<article id="SECONDS_IN_A_MINUTE">
<p>
<span class="header">SECONDS_IN_A_MINUTE</span>
<code>
<span class="util_class">util::</span><span class="func_name">SECONDS_IN_A_MINUTE</span>
</code>
</p>
<p>The number of seconds in a minute, useful for making code more verbose</p>
</article>
<article id="SECONDS_IN_AN_HOUR">
<p>
<span class="header">SECONDS_IN_AN_HOUR</span>
<code>
<span class="util_class">util::</span><span class="func_name">SECONDS_IN_AN_HOUR</span>
</code>
</p>
<p>The number of seconds in an hour, useful for making code more verbose</p>
</article>
<article id="SECONDS_IN_A_DAY">
<p>
<span class="header">SECONDS_IN_A_DAY</span>
<code>
<span class="util_class">util::</span><span class="func_name">SECONDS_IN_A_DAY</span>
</code>
</p>
<p>The number of seconds in a day, useful for making code more verbose</p>
</article>
<article id="SECONDS_IN_A_WEEK">
<p>
<span class="header">SECONDS_IN_A_WEEK</span>
<code>
<span class="util_class">util::</span><span class="func_name">SECONDS_IN_A_WEEK</span>
</code>
</p>
<p>The number of seconds in a week (7 days), useful for making code more verbose</p>
</article>
<article id="SECONDS_IN_A_MONTH">
<p>
<span class="header">SECONDS_IN_A_MONTH</span>
<code>
<span class="util_class">util::</span><span class="func_name">SECONDS_IN_A_MONTH</span>
</code>
</p>
<p>The number of seconds in a month (30 days), useful for making code more verbose</p>
</article>
<article id="SECONDS_IN_A_YEAR">
<p>
<span class="header">SECONDS_IN_A_YEAR</span>
<code>
<span class="util_class">util::</span><span class="func_name">SECONDS_IN_A_YEAR</span>
</code>
</p>
<p>The number of seconds in a year (365 days), useful for making code more verbose</p>
</article>
</section>
<h2 id="changelog">Change Log</h2>
<!--
Find: `(.*?)`
Replace: <code>\1<\/code>
Find: \[(.*?)\]\((.*?)\)
Replace: <a href="\2">\1</a>
-->
<p class="desc-text">
<span class="header">1.1.0</span><br>
This release introduces a deprecation notice for <code>slugify()</code>, please update your code.<br><br>
- Added a cryptographically secure random string function <code>secure_random_string</code>. Thanks to @abhimanyusharma003 via <a href="https://github.com/brandonwamboldt/utilphp/pull/53">Pull Request #53</a><br>
- Added <code>limit_characters</code> and <code>limit_words</code> functions. Thanks to @abhimanyusharma003 via <a href="https://github.com/brandonwamboldt/utilphp/pull/55">Pull Request #55</a><br>
- Added <code>rmdir</code> method to recursively delete a directory. Thanks to @ARACOOOL via <a href="https://github.com/brandonwamboldt/utilphp/pull/56">Pull Request #56</a><br>
- Added <code>set_executable</code>, <code>set_readable</code>, <code>set_writable</code>, <code>directory_size</code>, <code>directory_contents</code> and <code>get_user_dir</code> functions. Thanks to @sergserg via <a href="https://github.com/brandonwamboldt/utilphp/pull/70">Pull Request #70</a><br>
- Changed parameter ordering for <code>slugify</code>, <code>$css_mode</code> is now the third argument. For backwards compatibility, the old order will still work but it will generate an <code>E_USER_DEPRECATED</code> warning. Thanks to @abhimanyusharma003 via <a href="https://github.com/brandonwamboldt/utilphp/pull/71">Pull Request #71</a><br>
- Added <code>match_string</code> method. Thanks to @abhimanyusharma003 via <a href="https://github.com/brandonwamboldt/utilphp/pull/72">Pull Request #72</a><br>
- Renamed internal methods (protected ones) for PSR-2 compliance<br>
- General performance improvements, code quality improvements, and increased unit test coverage (special thanks to @hopeseekr for getting us near 100% coverage)<br>
</p>
<p class="desc-text">
<span class="header">1.0.7</span><br>
- Added <code>fix_broken_serialization</code> to fix broken serialized strings (Thanks to @hopeseekr via <a href="https://github.com/brandonwamboldt/utilphp/pull/48">Pull Request #48</a>)<br>
- Fixed <code>get_current_url</code> appending port 80 or 443 when not needed (Thanks to @scottchiefbaker via <a href="https://github.com/brandonwamboldt/utilphp/pull/49">Pull Request #49</a>)<br>
- <code>var_dump</code> can now handle recursive data structures without crashing<br>
- <code>var_dump</code> code was minified and cleaned up<br>
- <code>array_flatten</code> was optimized (thanks to @hopeseekr via <a href="https://github.com/brandonwamboldt/utilphp/pull/47">Pull Request #47</a>)<br>
- <code>remove_accents</code> was completely rewritten and is now ~4x faster<br>
</p>
<p class="desc-text">
<span class="header">1.0.6</span><br>
- Added <code>start_with</code> function<br>
- Added <code>ends_with</code> function<br>
- Added <code>str_contains</code> function<br>
- Added <code>str_icontains</code> function<br>
- Added <code>get_file_ext</code> function<br>
- Fixing permissions on the files &amp; directories<br>
- Fixing a bug with the include path of util.php<br>
</p>
<p class="desc-text">
<span class="header">1.0.5</span><br>
- <a href="https://github.com/brandonwamboldt/utilphp/issues/29">Issue #29</a> Fixed error in <code>var_dump</code> if mbstring extension wasn't present<br>
- Adding Composer support<br>
- Updating license from GPL to MIT<br>
- Adding Changelog to project<br>
- Bumping minimum version to PHP 5.3.3<br>
</p>
<p class="desc-text">
<span class="header">1.0.4</span><br />
- <a href="https://github.com/brandonwamboldt/utilphp/issues/22">Issue #22</a> Removed all superglobal *_get functions, you can use the modified <code>array_get</code> now<br />
- <a href="https://github.com/brandonwamboldt/utilphp/issues/22">Issue #22</a> Modifed the behaviour of <code>array_get</code>, see documentation<br />
- <a href="https://github.com/brandonwamboldt/utilphp/pull/21">Pull Request #21</a> Added multibyte support to html* functions<br />
- <a href="https://github.com/brandonwamboldt/utilphp/issues/9">Issue #9</a> Removed the <code>str_to_utf8</code> function<br />
- <a href="https://github.com/brandonwamboldt/utilphp/issues/3">Issue #3</a> Removed the absint function<br />
- Removed declare() from util.php to avoid errors<br />
- Updated PHPUnit tests to use PHPUnit 3.6
</p>
<p class="desc-text">
<span class="header">1.0.3</span><br />
- <a href="https://github.com/brandonwamboldt/utilphp/issues/16">Issue #16</a> Improved performance of <code>slugify</code><br />
- <a href="https://github.com/brandonwamboldt/utilphp/issues/14">Issue #14</a> Modified the regex for <code>seems_utf8</code> to be more accurate<br />
- <a href="https://github.com/brandonwamboldt/utilphp/issues/13">Issue #13</a> Changed <code>validate_email</code> to be wrapper for <code>filter_var</code><br />
- Added 'ok' to the list of yes words for <code>str_to_bool</code><br />
- <code>str_to_bool</code> matches no followed by any number of 'o's
</p>
<p class="desc-text">
<span class="header">1.0.2</span><br />
- <a href="https://github.com/brandonwamboldt/utilphp/issues/12">Issue #12</a> <code>get_current_url</code> now includes the port and user/password if required<br />
- <a href="https://github.com/brandonwamboldt/utilphp/issues/11">Issue #11</a> <code>human_time_diff</code> now uses the DateTime functions<br />
- <a href="https://github.com/brandonwamboldt/utilphp/issues/10">Issue #10</a> <code>is_https</code> no longer checks the port as well
</p>
<p class="desc-text">
<span class="header">1.0.1</span><br />
- <a href="https://github.com/brandonwamboldt/utilphp/issues/7">Issue #7</a> Added the <code style="color:#DA72F3">$trust_proxy_headers</code> parameter to <code>get_client_ip</code><br />
- <a href="https://github.com/brandonwamboldt/utilphp/issues/6">Issue #6</a> Removed <code>is_isset</code> as the function did not work as intended<br />
- <a href="https://github.com/brandonwamboldt/utilphp/issues/6">Issue #6</a> Removed <code>is_empty</code> as it is redundant, use ! with function calls instead<br />
- Fixed a bug with <code>get_gravatar</code>
</p>
<p class="desc-text">
<span class="header">1.0.0</span><br />
Initial release of util.php.
</p>
</div>
</div>
</body>
</html>