Hello,
I have a file with contents like this:
text here
asdfasdf
asdf
f
asdfsdf
<!-- #startpart -->
stuff goes here
<!-- #endpart -->
adsf
adsf
ffd
ff
df
I am trying to write some php code that will allow me to check and see if the file contains the a valid pair of "startpart" and "endpart" comments. (There can be an unknown number of spaces between each part of the HTML comments. The "#startpart" comment might also have some other junk in it like this: "<!-- #startpart anything else here -->". However, only the "<!-- #startpart -->" portion is actually required and should be checked if it exists.)
Does anyone know how to do this?
Here's what I have so far:
PHP Code:
$lines = file($file);
$count = 0;
for($x=0; $x<count($lines); $x++){ // For each line in the file..
/* The \b in the pattern indicates a word boundary, so only the distinct
word is matched, and not a word partial.
The "i" after the pattern delimiter indicates a case-insensitive search
*/
if (preg_match("/\b<!--\b(.*)\b#Startpart\b(.*)\b-->\b/i", $lines[$x])) {
$count += 1;
}
if (preg_match("/\b<!--\b(.*)\b#EndPart\b(.*)\b-->\b/i", $lines[$x])) {
$count += 1;
}
}
if(!($count%2) && $count>0) {
return true; // At least one *pair* of valid start/end tags was found.
}
else {
return false; // No valid tags were found, or tags were not in pairs.
}
However, this doesn't work. I'm not an expert with regex, so I'm hoping someone can help?
Bookmarks