I have 15 years of php coding under my belt, yet for the life of me, I cannot get preg_match_all to reliably count occurrences of a string that it locates. According to my many hours of research on this question, the following should locate all instances in which a string begins with two periods or full stops.
What is the value of $str when this preg_match_all is encountered? Do realize that you are using ^ at the beginning of the pattern, which means it will only match if it is at the start of the string. Are you only getting count coming back as 1?
Try removing the starting ^ and see if suddenly you get all your matches counted.Also, $matches is going to be an array where the zero index will contain all your matches. Try using count($matches[0])
$pattern="/\.\.[^.]/"; //<-- Notice we remove the starting ^ anchor character
$str="..h ..t"; //<-- Two matches should be here
preg_match_all($pattern, $str, $matches);
$thisCount=count($matches[0]); //<-- Notice we access array element 0
echo $thisCount; //<-- Returns 2
Here is the code I am using. The result for $thisCount is 0, whereas I assume that it should be 1.
$pattern=“/^..[^.]”;
$str=“…and so on.”
preg_match_all($pattern, $str, $matches);
$thisCount=count($matches);
Sorry, it should be
$pattern=“/^..[^.]/”; however that flags every line, even when it does not contain periods. I’m sure I’m making an obvious mistake somewhere, but I’ve yet to see it.
Basically, I’m working with a subtitle file that I’ve split using explode(chr(13),$subFile);
So I’m scanning each line, trying to flag double periods – without flagging single periods or triple periods. For this purpose, I wanted to flag lines that begin with “..” and lines that contain “..” and lines that end with “..” – thinking that each of these instances may require a separate regex.
Hmm. It looks like the forum software has changed my TWO ellipses to THREE ellipses automatically, so it makes it hard for me to show you what I’m talking about.
I see your coding. But won’t that also count ellipses, containing THREE dots? The goal of the script is to find two dots, since they should either really be three dots or one dot to be correct in a subtitle file.
Okay, thanks for your help! I think you’ve solved the problem I’ve asked about. Your code does work for me. I’ll need to tweak the regex to do exactly what I want, but that’s another issue, and I think I can figure that one out.