前瞻功能

Lookahead function

我有这个

87||2|#88||4|#89|

87||1|#88||3|#89|

95||1|#88||1|#89|

或其他变量数据。每次有 ...|#88||number|...),我都需要捕获 "number" 和 "print"。

根据收到的一些建议,我写了这个,但结果仍然不正确。

function caratteristiche1($property_bedrooms) {
    $new = preg_match("/\|88\|\|(\d+?)\b/", $property_bedrooms);
    print_r($new);
}

我能做什么?

您需要调用 preg_match() 的重载版本,它接受包含匹配项的数组作为第三个参数。您匹配的数字(在括号中)将出现在 $results 数组的 second 位置。只需访问此值,您就可以开始了:

function caratteristiche1($property_bedrooms) {
    $new = preg_match("/\|#88\|\|(\d+)/", $property_bedrooms, $results);
    print_r($results[1]);
}

顺便说一下,$results[0] 包含完整的输入字符串。

如果您想测试工作正则表达式,请单击下面的 link。

Regex101

好的,谢谢蒂姆! 你的正则表达式很好。正确的函数是:

function caratteristiche1($property_bedrooms) {
$new = preg_match('/\|#88\|\|(\d+)/', $property_bedrooms, $results);
print_r($results[1]);
}

感谢您的宝贵时间!