把头发拉出来:
我试图在这些词之间捕捉文字,并且总是得到不同的结果。
当前代码:
preg_match_all("/ID:([0-9A-Za-z]+)/",$data,$ids);
if (count($ids[0]) > 1) {
$data = str_replace($ids[0][1],"",$data);
}
preg_match("/ORIG:(.*)ID:.*/",$data,$matches);
$name = $matches[1];
if (FALSE !== $heh = strpos($name,"SND BK:")) {
$name = substr($name,0,$heh);
}
示例数据:
ORIG:FIRSTNAME LASTNAME ID:123123123 SND BK:TIB BANK BANK BANK DET:MEMO
ORIG:FIRSTNAME LASTNAME ID:123123123 SND BK:TIB BANK BANK BANK ID:123123123 DET:MEMO
ORIG:FIRSTNAME LASTNAME ID:123123123 DET:MEMO
我需要完全捕获ORIG
和DET
之间的数据。每当我在字段中有两个ID时,我一直在制作的RegEx总是会得到一个ID。有时甚至根本没有捕获ORIG
。
任何帮助表示赞赏。感谢。
答案 0 :(得分:0)
也许我真的不明白你在这里尝试了什么,但如果你使用以下内容,你最终将拥有orig和det之间的所有东西。
(ORIG:)(*)(DET)
示例:
preg_match_all('/(ORIG:)(.*)(DET)/',
'ORIG:FIRSTNAME LASTNAME ID:123123123 SND BK:TIB BANK BANK BANK DET:MEMO',
$result);
var_dump($result);
array(4) {
[0]=>
array(1) {
[0]=>
string(66) "ORIG:FIRSTNAME LASTNAME ID:123123123 SND BK:TIB BANK BANK BANK DET"
}
[1]=>
array(1) {
[0]=>
string(5) "ORIG:"
}
[2]=>
array(1) {
[0]=>
string(58) "FIRSTNAME LASTNAME ID:123123123 SND BK:TIB BANK BANK BANK "
}
[3]=>
array(1) {
[0]=>
string(3) "DET"
}
}
答案 1 :(得分:0)
如果这些数据分别在一行,我建议添加一个锚并使用多行:
<?php
$subject = <<<SUBJ
ORIG:FIRSTNAME FIRST LASTNAME ID:123123123 SND BK:TIB BANK BANK BANK DET:MEMO
ORIG:FIRSTNAME SECOND LASTNAME ID:123123123 SND BK:TIB BANK BANK BANK ID:123123123 DET:MEMO
ORIG:FIRSTNAME THIRD LASTNAME ID:123123123 DET:MEMO
SUBJ;
$pattern = ' /^ORIG:(.*)DET:/m';
preg_match_all($pattern, $subject, $matches);
print_r($matches);
?>
返回:
Array
(
[0] => Array
(
[0] => ORIG:FIRSTNAME FIRST LASTNAME ID:123123123 SND BK:TIB BANK BANK BANK DET:
[1] => ORIG:FIRSTNAME SECOND LASTNAME ID:123123123 SND BK:TIB BANK BANK BANK ID:123123123 DET:
[2] => ORIG:FIRSTNAME THIRD LASTNAME ID:123123123 DET:
)
[1] => Array
(
[0] => FIRSTNAME FIRST LASTNAME ID:123123123 SND BK:TIB BANK BANK BANK
[1] => FIRSTNAME SECOND LASTNAME ID:123123123 SND BK:TIB BANK BANK BANK ID:123123123
[2] => FIRSTNAME THIRD LASTNAME ID:123123123
)
)