[英]Split text file in to array using regular expression PHP
我在字符串中有一些更改日志文本,我想将每个更改日志条目拆分为一个数组。
这是变量中的更改日志文本示例 - $changelog_txt
:
version 4.4.6 ( updated 05-08-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.
version 4.4.5 ( updated 05-01-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.
因此,生成的 output 将是一个如下所示的数组:
array(
[0] => version 4.4.6 ( updated 05-08-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.
[1] => version 4.4.5 ( updated 05-01-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.
)
任何人都可以提供帮助或提供任何建议吗?
您可以使用preg_split()来创建一个数组。
<?php
$str = <<<EOD
version 4.4.6 ( updated 05-08-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.
version 4.4.5 ( updated 05-01-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.
EOD;
$keywords = array_map('trim',preg_split("/^\s*$/m", $str));
print_r($keywords);
?>
工作演示: https://3v4l.org/1mpZH
正则表达式解释:
/^\s*$/m
^
在行首断言 position
\s*
匹配任何空白字符(等于[\r\n\t\f\v ]
)
*
量词——在零次和无限次之间匹配,尽可能多次,根据需要回馈(贪婪)
$
在行尾断言 position 全局模式标志
m
修饰符:多行。 导致^
和$
匹配每行的开始/结束(不仅是字符串的开始/结束)
你可以 preg_replace_callback() 插入你自己的分隔符,然后explode()。
<?php
$oldString ='version 4.4.6 ( updated 05-08-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.
version 4.4.5 ( updated 05-01-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.';
$newString = preg_replace_callback('(version [0-9].[0-9].[0-9][0-9]?)', function($matches){return 'myUnlikelyDelimiter'.$matches[0];}, $oldString);
$array = explode('myUnlikelyDelimiter', $newString);
var_dump($array);
?>
<?php
$input_lines = <<<EOD
version 4.4.6 ( updated 05-08-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.
version 4.4.5 ( updated 05-01-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.
EOD;
preg_match_all('/(^[\s\S]*(?:\r*\n{2}))/U', $input_lines, $output_array);
print_r($output_array);
结果:
Array
(
[0] => Array
(
[0] => version 4.4.6 ( updated 05-08-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.
)
[1] => Array
(
[0] => version 4.4.6 ( updated 05-08-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.
)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.