我有这段代码,想从两个特定行之间的文本文件中提取数据。我想提取这两行的黑白部分。TEXT文件示例在这里
---
- ID: some random id
\_______________________________\_
HELLO
This is an example text.
I AM SECTION 1
\_______________________________\_
HELLO
This is an example text.
I AM SECTION 2
\_______________________________\_
HELLO
This is an example text.
I AM SECTION 3
\_______________________________\_
hello
this is example text here
and i am section 4
在这里,我有一些与这些行匹配的代码,但是没有找到如何从文本文件中提取包括最后一节在内的每个节。
并需要这样的输出:
[0] => ' HELLO
This is an example text.
I AM SECTION 1',
[1] => ' HELLO
This is an example text.
I AM SECTION 2',
[2] => ' HELLO
This is an example text.
I AM SECTION 3',
[3] => ' HELLO
This is an example text.
I AM SECTION 4',
public static function find_section_in_file($file = '', $directory = '')
{
$response = ['error' => true, 'section' => NULL];
if (isset($file) && isset($directory)) {
$handle = fopen($directory."\\".$file, "r");
$section = [];
if ($handle) {
while (($line = fgets($handle)) !== false) {
$new_line = trim(preg_replace('/\s+/', ' ', $line));
$start = self::startsWith($new_line, '\__');
$end = self::endsWith($new_line, '_\_');
if ($start && $end){
array_push($section, $line);
}
}
fclose($handle);
$response = ['error' => false, 'section' => $section];
}
//need To write Query to save section in DB
}
return $response;
}
您可以匹配所有不以反斜杠/下划线开头的行,并捕获捕获组1中的行。
^\h*\\_+\\_\R((?:.*\R(?!\h*\\_+\\_).*)*)
说明
^
字符串开始\h*\\_+\\_\R
比赛0+水平空白字符,\
,1个+下划线\
,_
和一个Unicode行序列(
捕获组1
(?:
非捕获组
.*\R
匹配整行和换行符(?!\h*\\_+\\_)
负向超前,断言该行不是以反斜杠/下划线开头.*
匹配整行匹配)*
关闭非捕获组并重复1次以上)
关闭捕获组例如
$re = '/^\h*\\\\_+\\\\_\R((?:.*\R(?!\h*\\\\_+\\\\_).*)*)/m';
$str = '---
- ID: some random id
\\_______________________________\\_
HELLO
This is an example text.
I AM SECTION 1
\\_______________________________\\_
HELLO
This is an example text.
I AM SECTION 2
\\_______________________________\\_
HELLO
This is an example text.
I AM SECTION 3
\\_______________________________\\_
hello
this is example text here
and i am section 4';
preg_match_all($re, $str, $matches);
print_r($matches[1]);
输出量
Array
(
[0] => HELLO
This is an example text.
I AM SECTION 1
[1] => HELLO
This is an example text.
I AM SECTION 2
[2] => HELLO
This is an example text.
I AM SECTION 3
[3] => hello
this is example text here
and i am section 4
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句