大佬教程收集整理的这篇文章主要介绍了最后一个 preg_match_all 结果后面的搜索位置 使用 PREG_OFFSET_CAPTURE使用 array_key_last,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要最后一场比赛之后的第一个位置。因为我想搜索 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","AI":false,"c":{"r":150,"g":20,"b":20}},someTingBehind
preg_match_all('/name":"([^"(]+)(?:\((\d*)\))?([^"]*)","Civ":"([^"]+)",.*?"Team":(\d+)/',$file_content,$matches,PREG_SET_ORDER,0);
var_dump($matches);
array(2) {
[0]=>
array(6) {
[0]=>
String(46) "name":"m (1196)","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)","Team":0"
[1]=>
String(5) "s "
[2]=>
String(3) "898"
[3]=>
String(0) ""
[4]=>
String(4) "ptol"
[5]=>
String(1) "0"
}
}
搜索并获取搜索后的内容。
如果此标志被传递,对于每个出现的匹配项,附加字符串偏移量(以字节为单位)也将被返回。
array_key_last 如果数组不为空,则返回数组的最后一个键;否则为NULL。
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,$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;
}
以上是大佬教程为你收集整理的最后一个 preg_match_all 结果后面的搜索位置 使用 PREG_OFFSET_CAPTURE使用 array_key_last全部内容,希望文章能够帮你解决最后一个 preg_match_all 结果后面的搜索位置 使用 PREG_OFFSET_CAPTURE使用 array_key_last所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。