在特定单词后找到一个单词

时间:2022-12-27 19:21:33

I am not so expert in the regular expression. I have created a function to find out a particular word after a first match word from an array. the code is

我对正则表达式并不那么专业。我创建了一个函数来查找数组中第一个匹配字后的特定单词。代码是

$searchkey1 = array('NUMBER', 'REF', 'TRANSACTION NUMBER');
$searchkey2 = array('CURRENT BALANCE RS.', 'NEW BALANCE IS');
$smsdata = "TRANSACTION NUMBER NER13082010132400255 TO RECHARGE 198 INR TO 9999999999 IS SUCCESSFUL. YOUR NEW BALANCE IS 8183.79 INR.";

function get_details($searchkey, $smsdata)
{
    foreach ($searchkey as $key) {
        $result = preg_match_all("/(?<=(" . $key . "))(\s\w*)/", $smsdata, $networkID);
        $myanswer = @$networkID[0][0];
    }
    return $myanswer;
}

echo get_details($searchkey1, $smsdata);
echo "<BR>";
echo get_details($searchkey2, $smsdata);

I am using this function to find out other words also. and it is working find for any other words except the decimal value. Here in my example code the return value is 11192 but i would like to get with decimal as 11192.75

我正在使用此功能来查找其他单词。并且它正在查找除十进制值之外的任何其他单词。在我的示例代码中,返回值是11192,但我想得到十进制为11192.75

Please rectify my error.

请纠正我的错误。

$result=preg_match_all("/(?<=(".$key."))(\s\w*)/",$smsdata,$networkID);

give the result NER13082010132400255 and 8183 //here the demical is ignore.

给出结果NER13082010132400255和8183 //这里的demical是忽略的。

$result=preg_match_all("/(?<=(".$key."))\s*(\d*\.?\d*)/",$smsdata,$networkID);

give the result "blank" and 8183.79 // here the first results is blank

给结果“空白”和8183.79 //这里第一个结果是空白的

My desire result is NER13082010132400255 and 8183.79

我的愿望结果是NER13082010132400255和8183.79

2 个解决方案

#1


2  

i don't believe \w covers period. use a mixture of \d and \.

我不相信\ w涵盖期限。使用\ d和\的混合物。

$result=preg_match_all("/(?<=(".$key."))(\s[\d\.]+)/",$smsdata,$networkID);

#2


1  

The OP's regexp captures the leading space too. @DevZer0's regexp captures the trailing dot too. This gives the balance only:

OP的正则表达式也捕获了领先的空间。 @ DevZer0的正则表达式也捕获了尾随点。这只给出了平衡:

$result=preg_match_all("/(?<=(".$key."))\s*(\d*\.?\d*)/",$smsdata,$networkID);

#1


2  

i don't believe \w covers period. use a mixture of \d and \.

我不相信\ w涵盖期限。使用\ d和\的混合物。

$result=preg_match_all("/(?<=(".$key."))(\s[\d\.]+)/",$smsdata,$networkID);

#2


1  

The OP's regexp captures the leading space too. @DevZer0's regexp captures the trailing dot too. This gives the balance only:

OP的正则表达式也捕获了领先的空间。 @ DevZer0的正则表达式也捕获了尾随点。这只给出了平衡:

$result=preg_match_all("/(?<=(".$key."))\s*(\d*\.?\d*)/",$smsdata,$networkID);