Hello,
I am having some difficulties with the split() function.
Here is the code for the split:
$fileName="file11.txt";
$fp=fopen($fileName,"r+");
$fileString=fread($fp, filesize($fileName));
$fileArray=split("/.*[a-zA-Z]{4}\s[0-9]{1,2}\t[a-zA-Z]*/",$fileString);
while(list($key,$val)=each($fileArray)){
echo "$key => $val <br />";
}
This returns an array with one element that contains the entire original string. According to the php documentation this occurs if the pattern is not found in the string.
What I did to see if this is indeed the case, that the pattern is not found in the string, I used preg_match_all().
// get file
$fileName="file11.txt";
$fp=fopen($fileName,"r+");
$fileString=fread($fp, filesize($fileName));
// test for match since split says the pattern may not be found in the string
if(!preg_match_all("/.*[a-zA-Z]{4}\s[0-9]{1,2}\t[a-zA-Z].*/",$fileString,$matches)){
echo "There is not match for your pattern<p />";
} else{
echo "There is a match for your string. ";
for ($i=0; $i< count($matches[0]); $i++) {
echo "matched: ".$matches[0][$i]."\n";
}
}
This returns the correct number of instances of pattern in string.
Therefore, I know the pattern is found in string, and multiple times.
It would make sense then that split() would find the string and return an array with more than one element.
Perhaps it is just somthing simple I am missing in the split() example of the code....