PHP 字符串处理:去除 JSON 中多余的逗号 作者: Chuwen 时间: 2024-10-11 分类: PHP 评论 在处理 JSON 数据时,有时可能会遇到一些格式问题,比如在对象或数组的最后一个元素后意外多出一个逗号。这类格式错误会导致 `json_decode()` 函数解析失败。因此,我们可以在解析之前,预先清理这些多余的逗号。 ## 1. 使用正则表达式处理多余逗号 在处理 JSON 数据时,可以使用正则表达式来匹配并移除对象或数组最后一个元素后面的逗号。以下是一个示例代码,展示如何通过 `preg_replace` 来解决这个问题: ```php
使用 docker inspect 和 jq 命令快速查看容器挂载目录、docker-compose.yaml 路径 作者: Chuwen 时间: 2024-07-31 分类: 神奇技巧 评论 关于 `jq` 命令的详细解释及安装教程:https://wangchujiang.com/linux-command/c/jq.html --- 语法: ```shell docker inspect | jq '.[0].Mounts' ``` 例如: ```shell shine@Shine-Win11:~/documents/self-projects/tmp$ docker inspect aa3f359ddc0e | jq '.[0].Mounts' [ { "Type": "bind", "Source": "/home/shine/documents/git/projects/docker-elk/elasticsearch/config/elasticsearch.yml", "Destination": "/usr/share/elasticsearch/config/elasticsearch.yml", "Mode": "ro,Z", "RW": false, "Propagation": "rprivate" }, { "Type": "volume", "Name": "docker-elk_elasticsearch", "Source": "/var/lib/docker/volumes/docker-elk_elasticsearch/_data", "Destination": "/usr/share/elasticsearch/data", "Driver": "local", "Mode": "z", "RW": true, "Propagation": "" } ] ``` ![](https://cdn.nowtime.cc/2024/07/31/3809813538.png) ## 查看 docker-compose.yaml 路径 语法: ```shell docker inspect | jq '.[0].Config.Labels' ``` 示例: ```shell shine@Shine-Win11:~/documents/git/projects/docker-elk$ docker inspect 8334b593b89a | jq '.[0].Config.Labels' { "com.docker.compose.config-hash": "d1f80fc712ff55f9f33eafc4842bf71f947a6584b27eb548bca0c0dc399bff8b", "com.docker.compose.container-number": "1", "com.docker.compose.depends_on": "", "com.docker.compose.image": "sha256:de7d209b219972d54381f0a7d18671d0c3aa1f3b4410a607cb82d34a08a97dc8", "com.docker.compose.oneoff": "False", "com.docker.compose.project": "frankenphp-typecho", "com.docker.compose.project.config_files": "/mnt/c/Users/Shine/Documents/Self/projects/frankenphp-typecho/docker-compose.yaml", "com.docker.compose.project.working_dir": "/mnt/c/Users/Shine/Documents/Self/projects/frankenphp-typecho", "com.docker.compose.replace": "3a7bd46c1737dd0ce32be2e6f4b246ca3e2778bbc83d105579c994e8fadc3a17", "com.docker.compose.service": "wordpress", "com.docker.compose.version": "2.24.7", "org.opencontainers.image.created": "2024-04-23T12:51:06Z", "org.opencontainers.image.description": "The modern PHP app server", "org.opencontainers.image.licenses": "MIT", "org.opencontainers.image.revision": "a9ebc3aeea590b45422a19510473955b372d09aa", "org.opencontainers.image.source": "https://github.com/dunglas/frankenphp", "org.opencontainers.image.title": "FrankenPHP", "org.opencontainers.image.url": "https://frankenphp.dev", "org.opencontainers.image.vendor": "Kévin Dunglas", "org.opencontainers.image.version": "v1.1.3" } ``` ![](https://cdn.nowtime.cc/2024/07/31/1733369616.png)
Windows Navicat Premium 16 直链下载(非 www.navicat.com.cn 网站) 作者: Chuwen 时间: 2024-07-30 分类: 软件 评论 ## Navicat Premium 16 官方(www.navicat.com)下载地址 * [Navicat Premium 16(简体中文)](https://www.navicat.com/download/direct-download?product=navicat160_premium_cs_x64.exe&location=1 "Navicat Premium(简体中文)") * [Navicat Premium 16(English)](https://www.navicat.com/download/direct-download?product=navicat160_premium_en_x64.exe&location=1 "Navicat Premium(简体中文)") ## Navicat Premium 17 官方(www.navicat.com)下载地址 * [Navicat Premium 17(简体中文)](https://www.navicat.com/download/direct-download?product=navicat170_premium_cs_x64.exe&location=1 "Navicat Premium(简体中文)") * [Navicat Premium 17(English)](https://www.navicat.com/download/direct-download?product=navicat170_premium_en_x64.exe&location=1 "Navicat Premium(简体中文)")
composer 永久忽略安装包时提示依赖 xx 扩展 作者: Chuwen 时间: 2024-07-02 分类: PHP 评论 ## 问题 在本地开发时,如果使用 composer 安装 composer 包经常会遇到这种提示: ``` Your requirements could not be resolved to an installable set of packages. Problem 1 - Root composer.json requires php ^8.3 but your php version (8.2.9) does not satisfy that requirement. Problem 2 - Root composer.json requires PHP extension ext-posix * but it is missing from your system. Install or enable PHP's posix extension. Problem 3 - Root composer.json requires PHP extension ext-rdkafka * but it is missing from your system. Install or enable PHP's rdkafka extension. Problem 4 - hhxsv5/laravel-s is locked to version v3.8.0 and an update of this package was not requested. - hhxsv5/laravel-s v3.8.0 requires ext-pcntl * -> it is missing from your system. Install or enable PHP's pcntl extension. Problem 5 - laravel/horizon is locked to version v5.24.4 and an update of this package was not requested. - laravel/horizon v5.24.4 requires ext-pcntl * -> it is missing from your system. Install or enable PHP's pcntl extension. Problem 6 - laravel/pail is locked to version v1.1.3 and an update of this package was not requested. - laravel/pail v1.1.3 requires ext-pcntl * -> it is missing from your system. Install or enable PHP's pcntl extension. To enable extensions, verify that they are enabled in your .ini files: - C:\Users\Shine\.pvm\versions\php-8.2\php.ini You can also run `php --ini` in a terminal to see which files are used by PHP in CLI mode. Alternatively, you can run Composer with `--ignore-platform-req=ext-posix --ignore-platform-req=ext-rdkafka --ignore-platform-req=ext-pcntl` to temporarily ignore these required extensions. You can also try re-running composer require with an explicit version constraint, e.g. "composer require dcat/laravel-admin:*" to figure out if any version is installable, or "composer require dcat/laravel-admin:^2.1" if you know which you need. Installation failed, reverting ./composer.json and ./composer.lock to their original content. ``` ![](https://cdn.nowtime.cc/2024/07/02/2434538828.png) 实际上在本机开发过程中通常不需要用到这些依赖扩展,或者你是 Windows 作为开发环境,那么像 `posix`, `pcntl` 扩展在 Windows 上是安装不上的,所以我都会加上 `--ignore-platform-reqs` 选项,来跳过对这些扩展的检查,但是每次装 composer 包都要手打这个选项实在是太过繁琐,有没有直接替我加上这个选项的方法? ## 过程 通过查阅[Composer 官方文档](https://getcomposer.org/doc/03-cli.md#composer-ignore-platform-req-or-composer-ignore-platform-reqs "Composer 官方文档"),确实有这么一个方法,设置一个环境变量 `COMPOSER_IGNORE_PLATFORM_REQS=1` 即可解决 ## 结果 Windows 添加环境变量 1. 按下 `Win` + `R` 输入 `sysdm.cpl` 然后按 `Enter` 2. 选择“环境变量” 3. 新建环境变量 4. 输入变量名 `COMPOSER_IGNORE_PLATFORM_REQS` 5. 输入变量值 `1` 6. 确定 -> 确定 -> 确定 7. 完成 ![](https://cdn.nowtime.cc/2024/07/02/3423099981.png)
记一次使用 Laravel Http Client 使用不当导致内存泄漏的问题 作者: Chuwen 时间: 2023-12-06 分类: PHP 评论 ## 背景 Laravel 的[ HTTP Client](https://laravel.com/docs/10.x/http-client " HTTP Client") 基于 Guzzle 二次封装,操作很方便,所以在业务代码上我就这么写了类似的代码 ```php $url = 'https://xxxx/xxx.json'; $json = Illuminate\Support\Facades\Http::get($url)->json(); // TODO 业务逻辑处理 ``` 项目使用了 [laravels](https://github.com/hhxsv5/laravel-s "laravels"),所以项目是常驻内存的,但是经过观察发 worker 内存会随着请求数量慢慢增加,并且不会自动回收内存,导致最终内存不足程序异常终止: ``` PHP Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 360448 bytes) in /Users/shine/work/xxxxxx-project/vendor/guzzlehttp/psr7/src/Utils.php on line 414 Symfony\Component\ErrorHandler\Error\FatalError Allowed memory size of 67108864 bytes exhausted (tried to allocate 360448 bytes) at vendor/guzzlehttp/psr7/src/Utils.php:414 410▕ }); 411▕ 412▕ try { 413▕ /** @var string|false $contents */ ➜ 414▕ $contents = stream_get_contents($stream); 415▕ 416▕ if ($contents === false) { 417▕ $ex = new \RuntimeException('Unable to read stream contents'); 418▕ } Whoops\Exception\ErrorException Allowed memory size of 67108864 bytes exhausted (tried to allocate 360448 bytes) at vendor/guzzlehttp/psr7/src/Utils.php:414 410▕ }); 411▕ 412▕ try { 413▕ /** @var string|false $contents */ ➜ 414▕ $contents = stream_get_contents($stream); 415▕ 416▕ if ($contents === false) { 417▕ $ex = new \RuntimeException('Unable to read stream contents'); 418▕ } +1 vendor frames 2 [internal]:0 Whoops\Run::handleShutdown() ``` ## 解决方案 通过研究框架代码,发现是没有正常关闭 Guzzle stream,导致内存一直累积无法回收 然后又在在 [vendor/laravel/framework/src/Illuminate/Http/Client/Response.php:247](https://github.com/laravel/framework/blob/10.x/src/Illuminate/Http/Client/Response.php#L247 "vendor/laravel/framework/src/Illuminate/Http/Client/Response.php:247") 位置发现有一个 `close()` 方法,但这个方法没有在任何地方使用,应该是想让你手动调用进行关闭 stream 所以将代码改成如下就可以了 ```php $url = 'https://xxxx/xxx.json'; $response = Illuminate\Support\Facades\Http::get($url); $json = $response->json(); // 关闭 stream $response->close(); // TODO 业务逻辑处理 ``` ## 附测试内存泄漏代码 ```php json(); printf("Count: %d\tPHP Memory: %.1fMB\n", $i++, memory_get_usage(true) / 1024.00 / 1024.00); } ``` ![](https://cdn.nowtime.cc/2023/12/06/138987942.png) ## 附测试修复内存泄漏后的代码 ```php body()) / 1024); $response->close(); } ``` ![](https://cdn.nowtime.cc/2023/12/06/3782614807.png)