Easy install with composer:
composer require jyoungblood/x-utilities
use VPHP\x;
require __DIR__ . '/vendor/autoload.php';
Sends a plain text or html email using the native PHP mail() function.
x::email_send([
'to' => '[email protected]',
'from' => '[email protected]',
'cc' => '[email protected]', // optional
'bcc' => '[email protected]', // optional
'reply-to' => '[email protected]', // optional
'subject' => 'Send me an email',
'html' => true, // optional, message will be sent as plain text unless this is true
'message' => 'Right now...<br /><br /><br /><b><u>RIGHT NOW</u></b>'
]);
Messages can be sent using the Mailgun API if Mailgun credentials are available as environment variables like this:
$_ENV['MAILGUN_API_KEY'] = 'key-f453654gg65sd6234r6rw5df6544e';
$_ENV['MAILGUN_DOMAIN'] = 'notifications.example.com';
or in your project's .env
file:
MAILGUN_API_KEY="key-f453654gg65sd6234r6rw5df6544e"
MAILGUN_DOMAIN="notifications.example.com"
Returns the address of the computer making the current request.
echo x::client_ip();
Returns a lowercase URL-safe version of a given string, substituting -
for spaces and punctuation.
echo x::url_slug('John User Name');
// john-user-name
Removes the protocol and trailing slashes from a given url, returning only the domain name.
echo x::url_strip('https://example.com/');
// example.com
Returns a valid URL, adding http://
if needed.
echo x::url_validate('example.com');
// http://example.com
The opposite of nl2br()
, replaces <br />
(and <br>
) html tags with newline (\n
) character.
echo x::br2nl('This is a <br /> multi-line <br /> string!');
// This is a \n multi-line \n string!
Turns an array of strings into a single string, separated by a vertical bar (|
) character.
echo x::array_encode(['Peter', 'Paul', 'Ringo', 'George']);
// Peter|Paul|Ringo|George
Turns a string separated by a vertical bar (|
) character into an array of strings.
$people = x::array_decode('Peter|Paul|Ringo|George');
print_r($people);
// ['Peter', 'Paul', 'Ringo', 'George']
Prints an array, object, or string in a stylized DOM container. Input type is automatically detected, and optional parameters can be used to customize the style of the container.
Typical usage:
x::console_log(['example' => 'array']);
With optional parameters:
x::console_log(['example' => 'array'], [
'format' => false, // removes all container formatting
'style' => [ // defines custom styles for container formatting
'font-size' => '16px',
'background' => 'blue',
'color' => 'yellow',
'padding' => '2.5rem',
'line-height' => '200%',
'custom' => 'font-style: italic'
]
]);
Same as console_log()
, but with with a die()
function called afterward. The same parameters are available for styling the container. Yes, it's kinda like Laravel's dd() method.
x::dd(['example' => 'array']);
Appends a string, array, or object to a given file. Input type is automatically detected and converted to plain text. Optional parameters can be used to customize fopen() mode and newline behavior.
x::file_write('A string to append to a file', 'data.txt');
Using custom parameters:
x::file_write(
['array_example' => 'An array to append to a file'],
'data.txt',
[
'mode' => 'w+', // define PHP fopen mode, default is 'a'
'line_beginning' => "\n- ", // prepend to beginning of input
'line_ending' => "", // append end of input, default is PHP_EOL
]
);
Abstraction for the native PHP error_log() function, appends a timestamp with a given string, array, or object to an error_log
file. Input type is automatically detected and converted to plain text.
x::error_log('Something bad happened.');