Php 两个索引之间的子字符串

Php 两个索引之间的子字符串,php,string,substring,Php,String,Substring,我的程序的目标是:将一个大字符串拆分为一个字符串数组,每个字符串都包含子字符串“zone”和下一个相同子字符串之间的文本段 我尝试的是: #Get file into a string $filecont=file_get_contents($filename); #Count all occurences of the substring, and get the positions of the substring into the array $arr $arr=strallpos($fi

我的程序的目标是:将一个大字符串拆分为一个字符串数组,每个字符串都包含子字符串“zone”和下一个相同子字符串之间的文本段

我尝试的是:

#Get file into a string
$filecont=file_get_contents($filename);
#Count all occurences of the substring, and get the positions of the substring into the array $arr
$arr=strallpos($filecont,"zone",0);
#The function strallpos does its job. The array $arr is now:
#Array ( [0] => 70 [1] => 148 [2] => 197 [3] => 316 [4] => 500 [5] => 637 [6] => 709 [7] => 748 [8] => 867 [9] => 878 [10] => 940 [11] => 990 [12] => 1154 [13] => 1321 [14] => 1440 [15] => 1561 [16] => 1684 [17] => 1695 [18] => 1759 [19] => 1811 [20] => 1926 [21] => 2045 [22] => 2168 [23] => 2285 )
#Find and return the substring
for ($i=0;($i+1)<=count($arr);$i++)
{       
    #Line 53 follows
    print (substr ($filecont,$arr($i),($arr($i+1)-$arr($i))));      
}
#将文件转换为字符串
$filecont=文件获取内容($filename);
#计算子字符串的所有出现次数,并将子字符串的位置放入数组$arr中
$arr=strallpos($filecont,“zone”,0);
#函数strallpos完成它的工作。阵列$arr现在是:
#数组([0]=>70[1]=>148[2]=>197[3]=>316[4]=>500[5]=>637[6]=>709[7]=>748[8]=>867[9]=>878[10]=>940[11]=>990[12]=>1154[13]=>1321[14]=>1440[15]=>1561[16]=>1684[17]=>1695[18]=>1759[19]=>1811[20]=>1926[22]=>
#查找并返回子字符串

对于($i=0;($i+1)来说,解决方案似乎是您需要使用explode,如下所示:

$zones = explode("zone", $filecont);

这将在$zones中存储一个数组,其中包含单词“zone”之间的所有字符串。请尝试告诉它是否有效:)

是否尝试获取字符串或字符串的位置?我已经以数组$arr的形式获取了子字符串的位置,其每个值都包含字符串出现的位置。现在我尝试返回这些位置之间的字符串段。
()
调用函数,
[]
访问数组标记。