I am attempting to parse a phrase separated with spaces and fill in a table (please see picture below). The parser has to check if each word
- ends with 3 alphabetic characters
- ends with 3 numeric characters
- has a special character
So far I am able to parse the phrase with the code below, however, I was able to get the first three characters checked and NOT the last three. I am also finding it difficult to include a check on special characters in a string into the table as well. Is there a way to check for the requirements above in a better way? Thank you in advance.
$words = 'The red fox eats meat, drinks and sleeps for 1000 hours';
$words=explode(' ', $input);//seperate the words and assign to an array
$wordCount=count($words);
<?php echo "<h1>Your Phrase has been Parsed</h1>"?>
<table>
<thead>
<td>Word</td>
<td>Length</td>
<td>Type</td>
</thead>
<tbody>
<?php
for($i=0; $i < $wordCount; $i++){
?>
<tr>
<td><?php $wordTable = $words[$i]; echo $wordTable; ?></td>
<td><?php echo sprintf("%02d", strlen($wordTable)) ?></td>
<td>
<?php
$WT1=0;
$WT2=0;
$WT3=0;
if (strlen($wordTable) >= 3){
for($j=0; $j<3; $j++){
if(ctype_alpha($wordTable{$j})){
$WT1++;
}
else if(is_numeric($wordTable{$j})){
$WT2++;
}
else if(preg_match('[@_!#$%^&*()<>?/\|}{~:;,]',$wordTable{$j})){
$WT3++;
}
}#for loop ends
}
?>
<?php
if($WT1 == 3){
echo "word ends with 3 alphabetic characters";
}
else if($WT2 == 3){
echo "Ends with 3 digits";
}
else if ($WT3==TRUE){
echo "word has a special character";
}
else{
echo "Undefined type";
}
?>
</td>
</tr>
<?php } ?>
</tbody>
</table>