两个索引之间的子串

时间:2013-07-17 09:12:25

标签: 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($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))));      
}

我得到的错误是:Fatal error: Function name must be a string in C:\wamp\www\dns.php on line 53

我做错了什么?我想对错误代码提出一些指示。

1 个答案:

答案 0 :(得分:2)

解决方案似乎是你需要使用explode,如:

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

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