PHP

laravel 处理 http_build_query() 参数中的 PHP_QUERY_RFC3986(URL编码协议)

2022年1月10日 laravel, PHP

原生 http_build_query() 方法定义:

function http_build_query (object|array $data, string $numeric_prefix = "", ?string $arg_separator = "&", int $encoding_type = PHP_QUERY_RFC1738)

指定使用 PHP_QUERY_RFC3986:

$query = http_build_query($query_data, null, null, PHP_QUERY_RFC3986);

laravel:

GuzzleHttp\Psr7\Query::build() 默认使用 PHP_QUERY_RFC3986

方法定义:

public static function build(array $params, $encoding = PHP_QUERY_RFC3986)

swoole不能通过添加php.ini扩展方式添加到PHP扩展中

2021年6月29日 PHP

Manually enabling Swoole via php.ini

On most systems Swoole can be enabled by directly editing your

php.ini

configuration file, you will want to add

extension=swoole

to the end of the file and then Swoole should be enabled.

Enabling Swoole via phpenmod

Some Linux distributions like Debian or Ubuntu use the PHP

mods-available

to load PHP extensions, in this case you can use

phpenmod

to enable Swoole.

 

一般系统 都可以通过添加扩展到php.ini的方式添加swoole支持,但我用的debian10+php7.4不支持,报如下错
 

PHP Warning: PHP Startup: Unable to load dynamic library 'swoole' (tried: /usr/lib/php/20190902/swoole (/usr/lib/php/20190902/swoole: cannot open shared object file: No such file or directory), /usr/lib/php/20190902/swoole.so (/usr/lib/php/20190902/swoole.so: undefined symbol: php_json_exception_ce)) in Unknown on line 0

 

只能通过

 phpenmod 方式添加

#!/bin/bash

# Create a Swoole extension PHP ini file, letting PHP know which modules to load
$ sudo bash -c "cat > /etc/php/<PHP_VERSION>/mods-available/swoole.ini << EOF
; Configuration for Swoole
; priority=25
extension=swoole
EOF"

# Enable the Swoole PHP extension only on CLI mode
$ sudo phpenmod -s cli swoole

# Optional: Enable Swoole for specific version of PHP CLI
$ sudo phpenmod -s cli -v 7.4 swoole

# Check if the Swoole extension has been enabled
$ php -m | grep swoole

 

You can also disable Swoole using

phpdismod

if you need to (this is not uninstalling, just turning Swoole off):

#!/bin/bash

# Turn off Swoole for CLI or a specific PHP install
sudo phpdismod -s cli swoole
sudo phpdismod -s cli -v 7.4 swoole

读CSV文件解决中文乱码问题

2021年1月7日 PHP

$file  = $_FILES[‘file’];
        $file_name = $file[‘tmp_name’];
        if($file_name == '') {
            $this->error("请选择要上传的csv文件");
        }
        // 以只读的方式打开文件
        $handle = fopen($file_name, 'r');
        if($handle === FALSE) {
            $this->error("打开文件资源失败");
        }
        // 删除上传文件
        unlink($file_name);
        // setlocale() 函数设置地区信息(地域信息)。
        @setlocale(LC_ALL, 'zh_CN');
        // CSV对应的字段名
        $csv_val = array('name', 'email', 'mobile', 'department', 'feedback_name', 'feedback_email', 'feedback_mobile', 'feedback_department', 'level');
        $data = array();
        while(($row = fgetcsv($handle)) !== FALSE) {
            //解决中文乱码
            $row = eval('return '.iconv('gbk','utf-8',var_export($row,true)).';');

            //这里需要检查和给的字段数是否一致,如果不一致,则不能写入
            $tmp_row = array();
            foreach ($csv_val as $k => $v) {
                $row_value = ltrim($row[$k], '`');
                $encode = mb_detect_encoding($row_value, array("ASCII", "UTF-8", "GB2312", "GBK", "BIG5"));
                if( $encode!="UTF-8" ){
                    $tmp_row[$v] = trim(iconv($encode,'UTF-8//IGNORE', $row_value));
                }else{
                    $tmp_row[$v] = trim($row_value);
                }
            }
            $data[] = $tmp_row;
        }
        // 关闭资源
        fclose($handle);

php处理CSV文件乱码之mb_detect_encoding

2020年2月29日 PHP

PHP在处理csv在数据导入导出方面,要比.xls高效很多。

实现一上传csv文件导入功能时,遇到上传文件编码不是UTF-8的情况,导致读取的数据乱码。如果csv文件是utf-8文件,可能导致excel不能正常识别,所以就遇到的问题。

搜索后发现mb_detect_encoding可以检测字符串编码,不一定百分百识别,至少能满足我的需求

代码片断:

            $tmp_row = array();
            foreach ($csv_val as $k => $v) {
                $row_value = ltrim($row[$k], '`');
                $encode = mb_detect_encoding($row_value, array("UTF-8", "GB2312", "CP936", "GBK"));
                if( $encode!="UTF-8" ){
                    $tmp_row[$v] = trim(iconv($encode,'UTF-8', $row_value));
                }else{
                    $tmp_row[$v] = trim($row_value);
                }
            }
            $data[] = $tmp_row;