jyoungblood / x-utilities
一组PHP工具函数,帮助您更快地完成任务。
1.2.3
2024-09-19 20:36 UTC
README
一组独立的工具函数集合,帮助您使用PHP更快地完成任务。
安装
使用composer轻松安装
composer require jyoungblood/x-utilities
use VPHP\x; require __DIR__ . '/vendor/autoload.php';
使用方法
x::email_send($parameters)
使用原生PHP的mail()函数发送纯文本或HTML电子邮件。
x::email_send([ 'to' => 'recipient@domain.com', 'from' => 'sender@example.com', 'cc' => 'cc-address@example.com', // optional 'bcc' => 'bcc-address@example.com', // optional 'reply-to' => 'reply-address@example.com', // 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>' ]);
如果Mailgun凭据作为环境变量可用,则可以使用Mailgun API发送消息,如下所示
$_ENV['MAILGUN_API_KEY'] = 'key-f453654gg65sd6234r6rw5df6544e'; $_ENV['MAILGUN_DOMAIN'] = 'notifications.example.com';
或在你项目的.env
文件中
MAILGUN_API_KEY="key-f453654gg65sd6234r6rw5df6544e"
MAILGUN_DOMAIN="notifications.example.com"
x::client_ip()
返回当前请求的计算机的地址。
echo x::client_ip();
x::url_slug($string)
返回给定字符串的小写URL安全版本,将空格和标点符号替换为-
。
echo x::url_slug('John User Name'); // john-user-name
x::url_strip($url)
从给定的URL中删除协议和尾部斜杠,仅返回域名。
echo x::url_strip('https://example.com/'); // example.com
x::url_validate($url)
返回一个有效的URL,如果需要,添加http://
。
echo x::url_validate('example.com'); // http://example.com
x::br2nl($string)
与nl2br()
相反,将<br />
(和<br>
)HTML标签替换为换行符(\n
)。
echo x::br2nl('This is a <br /> multi-line <br /> string!'); // This is a \n multi-line \n string!
x::array_encode($array)
将字符串数组转换为单个字符串,使用竖线(|
)字符分隔。
echo x::array_encode(['Peter', 'Paul', 'Ringo', 'George']); // Peter|Paul|Ringo|George
x::array_decode($string)
将用竖线(|
)字符分隔的字符串转换为字符串数组。
$people = x::array_decode('Peter|Paul|Ringo|George'); print_r($people); // ['Peter', 'Paul', 'Ringo', 'George']
x::console_log($input, $parameters)
在样式化的DOM容器中打印数组、对象或字符串。输入类型会自动检测,并且可以使用可选参数来自定义容器的样式。
典型用法
x::console_log(['example' => 'array']);
使用可选参数
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' ] ]);
x::dd($input, $parameters)
与console_log()
相同,但在之后调用die()
函数。可用的参数与用于样式化容器的参数相同。是的,有点像Laravel的dd()方法。
x::dd(['example' => 'array']);
x::file_write($input, $target_filename, $parameters)
将字符串、数组或对象追加到指定的文件。输入类型会自动检测并转换为纯文本。可以使用可选参数来自定义fopen()模式和换行行为。
x::file_write('A string to append to a file', 'data.txt');
使用自定义参数
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 ] );
x::error_log($input, $parameters)
原生PHP的error_log()函数的抽象,将时间戳和给定字符串、数组或对象追加到error_log
文件。输入类型会自动检测并转换为纯文本。
x::error_log('Something bad happened.');