定制 zoliszabo/php-mlstring 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

zoliszabo/php-mlstring

最新稳定版本:v2.0.0

Composer 安装命令:

composer require zoliszabo/php-mlstring

包简介

A pretty simple multiline strings library

README 文档

README

A pretty simple multi-line strings library.

Are you tired of using:

  • long (loooooooong) strings with \n and \r\n?
  • concatenation to make your strings readable?
  • heredoc syntax with its crazy indentation rules?

👉 This library allows you to use multi-line strings in a more readable way.

👎 Instead of this:

$longString = "This is a long string\n"
    . "that spans multiple lines\n"
    . "and uses concatenation to be readable.";

👎 or this:

$longString = <<<EOD
This is a long string
that spans multiple lines
and uses heredoc syntax to be readable.
EOD;

👍 You can use this:

$longString = MLString(
    "This is a long string",
    "that spans multiple lines",
    "and uses MLString to be readable."
);

Installation

You can install this library using Composer. Run the following command in your terminal:

composer require zoliszabo/mlstring

Usage

  1. The class constructor accepts any number of string arguments, which will be concatenated together with a newline character (PHP_EOL) between them:

    use MLString\MLString;
    
    $mlString = new MLString(
        "This is a long string",
        "that spans multiple lines",
        "and uses MLString to be readable."
    );
    
    echo $mlString; // Outputs the multi-line string
  2. Alternatively, you can use the MLString::of() static method to create an instance of MLString:

    use MLString\MLString;
    
    $mlString = MLString::of(
        "This is a long string",
        "that spans multiple lines",
        "and uses MLString to be readable."
    );
    
    echo $mlString; // Outputs the multi-line string
  3. Finally, you can also use the MLString function as a shortcut:

    use function MLString\MLString;
    
    $mlString = MLString(
        "This is a long string",
        "that spans multiple lines",
        "and uses MLString to be readable."
    );
    
    echo $mlString; // Outputs the multi-line string

Real-world usage example

use function MLString\MLString;

throw new \Exception(
    MLString(
        "This is a multi-line exception message.",
        "It can span multiple lines for better readability.",
        "You can use it just like a regular string."
    )
);

Custom glue strings

Starting version 2, you can customize the string that is used to join the lines together by calling the withGlue(...), withNewLine() or withSpace() methods on the MLString instance:

use MLString\MLString;

$mlString = MLString::of(
    "This is a long string",
    "that spans multiple lines",
    "and uses MLString to be readable.",
);
echo $mlString->withGlue(' | ');
// Outputs: "This is a long string | that spans multiple lines | and uses MLString to be readable."

echp $mlString->withSpace();
// Outputs: "This is a long string that spans multiple lines and uses MLString to be readable."

echo $mlString->withNewLine();
// Outputs the original string with new lines

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-18

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固