• 技术文章 >头条

    【整理推荐】值得收藏的8个实用PHP库

    青灯夜游青灯夜游2022-12-19 19:53:12原创373
    PHP中文网为同学们整理了github上Star比较高的8个实用的PHP库,提升开发效率。

    1.pinyin(中文转拼音工具)

    项目地址:https://github.com/overtrue/pinyin

    基于 CC-CEDICT 词典的中文转拼音工具,更准确的支持多音字的汉字转拼音解决方案,示例代码:

    se OvertruePinyinPinyin;
    
    $pinyin = new Pinyin();
    
    $pinyin->convert('带着希望去旅行,比到达终点更美好');
    // ["dai", "zhe", "xi", "wang", "qu", "lv", "xing", "bi", "dao", "da", "zhong", "dian", "geng", "mei", "hao"]
    
    $pinyin->convert('带着希望去旅行,比到达终点更美好', PINYIN_UNICODE);
    // ["dài","zhe","xī","wàng","qù","lǚ","xíng","bǐ","dào","dá","zhōng","diǎn","gèng","měi","hǎo"]
    
    $pinyin->convert('带着希望去旅行,比到达终点更美好', PINYIN_ASCII);
    //["dai4","zhe","xi1","wang4","qu4","lv3","xing2","bi3","dao4","da2","zhong1","dian3","geng4","mei3","hao3"]

    2.php-curl-class(PHP cURL 库)

    项目地址:https://github.com/php-curl-class/php-curl-class

    该开源项目封装了 PHP 的 cURL 库,使得发送 HTTP 请求变得简单。适用于需要 PHP 爬虫或者其它模拟 HTTP 访问的情况,示例代码:

    <?php
    // 获取豆瓣电影示例
    require '../vendor/autoload.php';
    use Curl\Curl;
    
    $curl = new Curl();
    $url = "https://movie.douban.com/j/search_subjects?type=movie&tag=%E8%B1%86%E7%93%A3%E9%AB%98%E5%88%86&sort=time&page_limit=20&page_start=1";
    $curl->get($url);
    $curl->setOpt(CURLOPT_SSL_VERIFYPEER, false);
    $curl->close();
    var_dump($curl->getResponse());exit;

    1.png

    3.parsedown(Markdown 解析库)

    项目地址:https://github.com/erusev/parsedown

    一个小而美的 PHP 的 Markdown 解析库。该库提供了标准 Markdown 文本转化成 HTML 字符串功能,并拥有良好的文档。它的主文件只有一个,除了 PHP 版本限制必须高于 5.3 外几乎无依赖,可通过 composer 引入,也可以直接使用 Parsedown.php 文件。该项目中使用大量正则表达式,可作为学习正则表达式的示例,并且有完整的单元测试。示例代码:

    $Parsedown = new Parsedown();
    echo $Parsedown->text('Hello _Parsedown_!'); # prints: <p>Hello <em>Parsedown</em>!</p>

    4.dompdf(HTML 转 PDF)

    项目地址:https://github.com/dompdf/dompdf

    一个 HTML 转 PDF 的 PHP 库。示例代码:

    // reference the Dompdf namespace
    use Dompdf\Dompdf;
    
    // instantiate and use the dompdf class
    $dompdf = new Dompdf();
    $dompdf->loadHtml('hello world');
    
    // (Optional) Setup the paper size and orientation
    $dompdf->setPaper('A4', 'landscape');
    
    // Render the HTML as PDF
    $dompdf->render();
    
    // Output the generated PDF to Browser
    $dompdf->stream();

    5.PHPWord(电商平台)

    项目地址:https://github.com/PHPOffice/PHPWord

    提供了读/写多种文档文件格式的 PHP 库。支持 Microsoft Office、富文本(RTF)等文档格式

    <?php
    require_once 'bootstrap.php';
    
    // 新建文档
    $phpWord = new \PhpOffice\PhpWord\PhpWord();
    
    // Adding an empty Section to the document...
    $section = $phpWord->addSection();
    // Adding Text element to the Section having font styled by default...
    $section->addText(
        '"Learn from yesterday, live for today, hope for tomorrow. '
            . 'The important thing is not to stop questioning." '
            . '(Albert Einstein)'
    );

    6.easy-sms(短信发送 )

    项目地址:https://github.com/overtrue/easy-sms

    短信发送 PHP 组件。特点:

    use Overtrue\EasySms\EasySms;
    
    $config = [
        // HTTP 请求的超时时间(秒)
        'timeout' => 5.0,
    
        // 默认发送配置
        'default' => [
            // 网关调用策略,默认:顺序调用
            'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class,
    
            // 默认可用的发送网关
            'gateways' => [
                'yunpian', 'aliyun',
            ],
        ],
        // 可用的网关配置
        'gateways' => [
            'errorlog' => [
                'file' => '/tmp/easy-sms.log',
            ],
            'yunpian' => [
                'api_key' => '824f0ff2f71cab52936axxxxxxxxxx',
            ],
            'aliyun' => [
                'access_key_id' => '',
                'access_key_secret' => '',
                'sign_name' => '',
            ],
            //...
        ],
    ];
    
    $easySms = new EasySms($config);
    
    $easySms->send(13188888888, [
        'content'  => '您的验证码为: 6379',
        'template' => 'SMS_001',
        'data' => [
            'code' => 6379
        ],

    7.YOURLS(短网址生成)

    项目地址:https://github.com/YOURLS/YOURLS

    完全免费的短网址服务。采用 PHP 编写的短网址服务,它完全开源可自行搭建服务,支持数据统计、地理位置、可视化等功能。

    2.gif

    8.php-console(PHP 命令行应用库)

    项目地址:https://github.com/inhere/php-console

    使用简单,功能全面的 PHP 命令行应用库。提供控制台参数解析、命令运行、颜色风格输出、 用户信息交互等功能

    3.png

    开班通知:

    php中文网第《22期PHP线上直播班》正式开始报名了!

    4.png

    授课形式:

    1.零基础开始,前端到后端,系统的学习!

    2.直播同步录播,内部群,老师作业批改,辅导解答,督促学习

    限额活动:

    1.前50名报名同学,即赠送三部实战课程,先到先得,送完为止!

    2.报名即赠送一套完整PHP课程(超值),课前学习!(限前10名)

    报名咨询↓↓↓

    QQ报名:27220243(钟老师)

    微信报名:phpcn01(月月老师)

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:php
    上一篇:三年面试经验分享:前端面试的四个阶段和三个决定因素 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • 怎么编译安装nginx和php• php pcntl fork失败怎么办• 服务器中怎么去掉index.php• php正则替换怎么实现• php怎么做到只有一个用户登录
    1/1

    PHP中文网