搜索位置落后于最后 preg_match_all 个结果
search position behind last preg_match_all result
我需要最后一场比赛之后的第一个位置。因为我想在 regex_match_all 后面的内容中搜索(在本例中 ,sometingBehind
)。
这可能无法(开箱即用)从 preg-match-all
获得
来源
start "settings":{"PlayerData":[{"Name":"m (1196)","Civ":"ptol","Team":0,"AI":f,"c":{"r":21,"g":55,"b":149}},{"Name":"s (898)","Civ":"p","Team":0,"AI":false,"c":{"r":150,"g":20,"b":20}},sometingBehind
我的preg_match_all
preg_match_all('/Name":"([^"(]+)(?:\((\d*)\))?([^"]*)","Civ":"([^"]+)",.*?"Team":(\d+)/'
, $file_content
, $matches
, PREG_SET_ORDER
, 0);
var_dump($matches);
我的 $matches 符合预期(缺少 endPos)
array(2) {
[0]=>
array(6) {
[0]=>
string(46) "Name":"m (1196)","Civ":"ptol","Team":0"
[1]=>
string(10) "m "
[2]=>
string(4) "1196"
[3]=>
string(0) ""
[4]=>
string(4) "ptol"
[5]=>
string(1) "0"
}
[1]=>
array(6) {
[0]=>
string(40) "Name":"s (898)","Civ":"ptol","Team":0"
[1]=>
string(5) "s "
[2]=>
string(3) "898"
[3]=>
string(0) ""
[4]=>
string(4) "ptol"
[5]=>
string(1) "0"
}
}
正在搜索并获取搜索后的内容。
使用PREG_OFFSET_CAPTURE
如果传递此标志,对于每个出现的匹配项,还将返回附加字符串偏移量(以字节为单位)。
使用array_key_last
array_key_last returns 如果数组不为空则数组的最后一个键;否则为 NULL。
需要 (PHP 7 >= 7.3.0, PHP 8)
preg_match_all('/Name":"([^"(]+)(?:\((\d*)\))?([^"]*)","Civ":"([^"]+)",.*?"Team":(\d+)/'
, $file_content
, $matches
, PREG_OFFSET_CAPTURE
, 0);
$contentBehind = getContentBehind($matches, $file_content);
echo contentBehind; # ,sometingBehind in this example
/**
* @param $matches
* @param $file_content
* using array_key_last (PHP 7 >= 7.3.0, PHP 8)
* @return string contentBehind
*/
function getContentBehind($matches, $file_content, $maxLength=100){
$last1ArrayValue = $matches[array_key_last($matches)];
$last2ArrayValue = $last1ArrayValue[array_key_last($last1ArrayValue)];
$lastKey = array_key_last($last2ArrayValue);
$lastPos = $last2ArrayValue[$lastKey];
$posBehindLastSearch = $lastPos + $last1ArrayValue[array_key_last($last1ArrayValue) - 1][$lastKey - 1];
$contentBehind = substr($file_content, $posBehindLastSearch, $maxLength);
return $contentBehind;
}
我需要最后一场比赛之后的第一个位置。因为我想在 regex_match_all 后面的内容中搜索(在本例中 ,sometingBehind
)。
这可能无法(开箱即用)从 preg-match-all
获得来源
start "settings":{"PlayerData":[{"Name":"m (1196)","Civ":"ptol","Team":0,"AI":f,"c":{"r":21,"g":55,"b":149}},{"Name":"s (898)","Civ":"p","Team":0,"AI":false,"c":{"r":150,"g":20,"b":20}},sometingBehind
我的preg_match_all
preg_match_all('/Name":"([^"(]+)(?:\((\d*)\))?([^"]*)","Civ":"([^"]+)",.*?"Team":(\d+)/'
, $file_content
, $matches
, PREG_SET_ORDER
, 0);
var_dump($matches);
我的 $matches 符合预期(缺少 endPos)
array(2) {
[0]=>
array(6) {
[0]=>
string(46) "Name":"m (1196)","Civ":"ptol","Team":0"
[1]=>
string(10) "m "
[2]=>
string(4) "1196"
[3]=>
string(0) ""
[4]=>
string(4) "ptol"
[5]=>
string(1) "0"
}
[1]=>
array(6) {
[0]=>
string(40) "Name":"s (898)","Civ":"ptol","Team":0"
[1]=>
string(5) "s "
[2]=>
string(3) "898"
[3]=>
string(0) ""
[4]=>
string(4) "ptol"
[5]=>
string(1) "0"
}
}
正在搜索并获取搜索后的内容。
使用PREG_OFFSET_CAPTURE
如果传递此标志,对于每个出现的匹配项,还将返回附加字符串偏移量(以字节为单位)。
使用array_key_last
array_key_last returns 如果数组不为空则数组的最后一个键;否则为 NULL。
需要 (PHP 7 >= 7.3.0, PHP 8)
preg_match_all('/Name":"([^"(]+)(?:\((\d*)\))?([^"]*)","Civ":"([^"]+)",.*?"Team":(\d+)/'
, $file_content
, $matches
, PREG_OFFSET_CAPTURE
, 0);
$contentBehind = getContentBehind($matches, $file_content);
echo contentBehind; # ,sometingBehind in this example
/**
* @param $matches
* @param $file_content
* using array_key_last (PHP 7 >= 7.3.0, PHP 8)
* @return string contentBehind
*/
function getContentBehind($matches, $file_content, $maxLength=100){
$last1ArrayValue = $matches[array_key_last($matches)];
$last2ArrayValue = $last1ArrayValue[array_key_last($last1ArrayValue)];
$lastKey = array_key_last($last2ArrayValue);
$lastPos = $last2ArrayValue[$lastKey];
$posBehindLastSearch = $lastPos + $last1ArrayValue[array_key_last($last1ArrayValue) - 1][$lastKey - 1];
$contentBehind = substr($file_content, $posBehindLastSearch, $maxLength);
return $contentBehind;
}