参考:https://www.php.net/manual/en/function.str-split.php

最近碰到一个问题,客户的英文地址太长,超出接口api字段长度,所以需要解析下语句分解发送。

例子:

$str = "Zaharna Fabrika Kukush 1 building 69 entrance B floor 3 apartment 312";
p(str_split_word_aware($str,32));die;

结果:

<pre style='position:relative;z-index:1000;padding:10px;border-radius:5px;background:#F5F5F5;border:1px solid #aaa;font-size:14px;line-height:18px;opacity:0.9;'>Array
(
    [0] => Zaharna Fabrika Kukush 1 
    [1] => building 69 entrance B floor 3 
    [2] => apartment 312 
)
</pre>

函数

function str_split_word_aware(string $string, int $maxLengthOfLine): array
{
    if ($maxLengthOfLine <= 0) {
        throw new RuntimeException(sprintf('The function %s() must have a max length of line at least greater than one', __FUNCTION__));
    }

    $lines = [];
    $words = explode(' ', $string);

    $currentLine = '';
    $lineAccumulator = '';
    foreach ($words as $currentWord) {

        $currentWordWithSpace = sprintf('%s ', $currentWord);
        $lineAccumulator .= $currentWordWithSpace;
        if (strlen($lineAccumulator) < $maxLengthOfLine) {
            $currentLine = $lineAccumulator;
            continue;
        }

        $lines[] = $currentLine;

        // Overwrite the current line and accumulator with the current word
        $currentLine = $currentWordWithSpace;
        $lineAccumulator = $currentWordWithSpace;
    }

    if ($currentLine !== '') {
        $lines[] = $currentLine;
    }

    return $lines;
}

解决 laravel-admin between datetime 假如数据库是时间戳int类型无法筛选。

laravel-admin默认的between->datetime(),查询默认是datetime类型,但是假如数据库是时间戳类型就会报错,又不想改底层文件的话可以试试加自定义筛选功能...

阅读全文

记录一个laravel-excel导出表格值为0导出excel显示空的解决方法。

最近在使用laravel-excel导出表格的时候,发现假如字段值为0的情况下,导出的excel中直接显示为空,找到一个方法解决,如下. 在laravel-excel的config配置中...

阅读全文

php实现redis延时队列

写了一个简单的类实现 使用方法 $class = new RedisDelayQueue(); $class->execute(); <?php namespace app\common\service; class RedisDelay { ...

阅读全文

欢迎留言