linus-codes / patternfiller
在模式中填充占位符
v1.0.1
2023-06-24 17:38 UTC
Requires
- php: >=8.0
This package is auto-updated.
Last update: 2024-09-24 20:23:38 UTC
README
Patternfiller是一个类,允许您在模式文本中用数据替换占位符。
示例
require_once __DIR__.'/vendor/linus-codes/patternfiller/src/PatternFiller.php'; use LinusCodes\patternfiller\PatternFiller; $regex = '/\[\(\$(.*?)\)\]/'; $one = 'Name: [($name)]<br>Parts:<br><div style="display:flex; flex-direction:column;">[($partsArray)]</div>Year: [($year)]'; $json = '{ "name": "Linus", "partsArray": [ { "format": "<div><div style=\"float: left; padding: 5px; width:80px; height:20px; background:rgb(220, 220, 220);\">[($number)]</div><div style=\"float:left; padding: 5px; width:80px; height:20px; background: rgb(190,190,190);\">[($partname)]</div><div style=\"float: left; padding: 5px; width:80px; height:20px; background:rgb(220, 220, 220);\">[($partprice)]</div></div>", "number": "123456", "partname": "Something", "partprice": "4,50$" }, { "format": "<div><div style=\"float: left; padding: 5px; width:80px; height:20px; background:rgb(220, 220, 220);\">[($number)]</div><div style=\"float:left; padding: 5px; width:80px; height:20px; background: rgb(190,190,190);\">[($partname)]</div><div style=\"float: left; padding: 5px; width:80px; height:20px; background:rgb(220, 220, 220);\">[($partprice)]</div></div>", "number": "101112", "partname": "Anything", "partprice": "12,20$" } ], "year": "2023" }'; $filler = new PatternFiller(); echo $filler->replacePlaceholder($one, json_decode($json, true), '/\[\(\$(.*?)\)\]/');
在这个示例中,占位符以'[(]'开头,以')]'结尾。中间的文本是占位符的名称。它被用来选择应插入的数据。您可以通过提供合适的正则表达式来定义自己的占位符。
您还可以为占位符定义一个数组,如所示
"partsArray": [ { "format": "<div><div style=\"float: left; padding: 5px; width:80px; height:20px; background:rgb(220, 220, 220);\">[($number)]</div><div style=\"float:left; padding: 5px; width:80px; height:20px; background: rgb(190,190,190);\">[($partname)]</div><div style=\"float: left; padding: 5px; width:80px; height:20px; background:rgb(220, 220, 220);\">[($partprice)]</div></div>", "number": "123456", "partname": "Something", "partprice": "4,50$" }, { "format": "<div><div style=\"float: left; padding: 5px; width:80px; height:20px; background:rgb(220, 220, 220);\">[($number)]</div><div style=\"float:left; padding: 5px; width:80px; height:20px; background: rgb(190,190,190);\">[($partname)]</div><div style=\"float: left; padding: 5px; width:80px; height:20px; background:rgb(220, 220, 220);\">[($partprice)]</div></div>", "number": "101112", "partname": "Anything", "partprice": "12,20$" } ]
在格式中,您可以为一维数组中的每个元素选择一个模式。该类还允许数组中的数组,数组的数组,以此类推...
使用 replacePlaceholder() 函数
$patternFiller->replacePlaceholder(string $origin, array $data, string $regex);
-$origin expects the pattern-text with placeholders
-$data expects an array of the needed data to fill the placeholders. Make shure both array keyvalues and placeholders are named correctly
-$regex expects a regular expression to mark placeholders as such
同名占位符将获得相同的值。
安装
composer require linus-codes/patternfiller