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
\nand\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
-
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
-
Alternatively, you can use the
MLString::of()static method to create an instance ofMLString: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
-
Finally, you can also use the
MLStringfunction 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
其他信息
- 授权协议: MIT
- 更新时间: 2025-07-18