PHP 字符串处理:去除 JSON 中多余的逗号 作者: Chuwen 时间: 2024-10-11 分类: PHP 评论 在处理 JSON 数据时,有时可能会遇到一些格式问题,比如在对象或数组的最后一个元素后意外多出一个逗号。这类格式错误会导致 `json_decode()` 函数解析失败。因此,我们可以在解析之前,预先清理这些多余的逗号。 ## 1. 使用正则表达式处理多余逗号 在处理 JSON 数据时,可以使用正则表达式来匹配并移除对象或数组最后一个元素后面的逗号。以下是一个示例代码,展示如何通过 `preg_replace` 来解决这个问题: ```php
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)
Laravels + DcatAdmin 使用问题 作者: Chuwen 时间: 2023-12-04 分类: PHP 评论 如果按照 Laravels 的文档所说的,在 `config/laravels.php` 文件的 `cleaners` 数组加上: ```php 'cleaners' => [ Hhxsv5\LaravelS\Illuminate\Cleaners\SessionCleaner::class, Hhxsv5\LaravelS\Illuminate\Cleaners\AuthCleaner::class, Hhxsv5\LaravelS\Illuminate\Cleaners\DcatAdminCleaner::class ], ``` 实际上还是会有问题的,比如我遇到的就是菜单栏不会渲染问题 如果要解决这个问题也很简单,参照 https://github.com/hhxsv5/laravel-s/issues/251#issuecomment-971224092 写一个清理累去清理。 以下是我根据这个 issues 摘录的操作步骤 ## 步骤 1. 创建如 `app/Cleaners` 文件夹 2. 新建一个文件 `app/Cleaners/DcatAdminCleaner.php` 内容为 ```php currentApp); $flushAdminState->handle(null); } } ``` 3. 将 `config/laravels.php` 的内容 `Hhxsv5\LaravelS\Illuminate\Cleaners\DcatAdminCleaner::class` 替换为 `App\Cleaners\DcatAdminCleaner::class` 即可 ![](https://cdn.nowtime.cc/2023/12/04/1914189827.png)
PHP 8.1 Enum (枚举) 根据名字取得枚举 作者: Chuwen 时间: 2023-09-21 分类: PHP 评论 灵感来源于:https://github.com/php/php-src/issues/9352#issuecomment-1216839118 ```php $enumClass * @param string $name * @return T|null */ function tryFromName(string $enumClass, string $name) { $constName = $enumClass . '::' . $name; return is_subclass_of($enumClass, UnitEnum::class) && defined($constName) ? constant($constName) : null; } // enum(Gender::UNKNOWN) var_dump(tryFromName(Gender::class, 'UNKNOWN')); // enum(Gender::MALE) var_dump(tryFromName(Gender::class, 'MALE')); // enum(Gender::FEMALE) var_dump(tryFromName(Gender::class, 'FEMALE')); // NULL var_dump(tryFromName(Gender::class, 'null')); ```