Using bash or php, how can I detect whether the last block of php in a file has a closing tag or not, regardless of trailing newlines or whitespace?
This is what I've got so far, but I can't figure out how to determine if what comes after the closing tag is more php or not.
#!/bin/bash
FILENAME="$1"
closed=false
# just checking the last 10 lines
# should be good enough for this example
for line in $(tail $FILENAME); do
if [ "$line" == "?>" ]; then
closed=true
else
closed=false
fi
done
if $closed; then
exit 1
else
exit 0
fi
I wrote a few tests with a test runner script.
#!/bin/bash
for testfile in $(ls tests); do
./closed-php.bash tests/$testfile
closed=$?
if [ $closed -eq 1 -a "true" == ${testfile##*.} ] ||
[ $closed -eq 0 -a "false" == ${testfile##*.} ]; then
echo "[X] $testfile"
else
echo "[ ] $testfile"
fi
done
You can clone these files, but this is what I've got so far.
.
├── closed-php.bash
├── test.bash
└── tests
├── 1.false
├── 2.true
├── 3.true
├── 4.true
├── 5.false
└── 6.false
FALSE :
<?php $var = 'value';
TRUE :
<?php $var = 'value'; ?>
TRUE :
<?php $var = 'value'; ?><!DOCTYPE>
TRUE :
<?php $var = 'value'; ?> <!DOCTYPE>
FALSE :
<?php $var = 'value'; ?> <!DOCTYPE> <?php $var = 'something';
FALSE :
<?php $var = 'value'; ?><?php $var = 'something';
I'm failing 3 & 4 because I can't figure out if what comes after the closing tag is more php or not.
[X] 1.false
[X] 2.true
[ ] 3.true
[ ] 4.true
[X] 5.false
[X] 6.false