1

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

  1. ends with 3 alphabetic characters
  2. ends with 3 numeric characters
  3. 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>
kingkwabs
  • 13
  • 4
  • You can have a read of [How can I get the last 7 characters of a PHP string?](https://stackoverflow.com/questions/10542310/how-can-i-get-the-last-7-characters-of-a-php-string) which may give you a quick way of getting the last 3 characters of a string. – Nigel Ren Aug 10 '20 at 15:10

3 Answers3

0

I see you already employ regex. I suggest extending that to the whole script:

if (strlen($wordTable) >= 3) {
    if (preg_match('/[a-zA-Z]{3}$/', $wordTable)) {
        //case 1
    } elseif (preg_match('/[\d]{3}$/', $wordTable)) {
        //case 2
    } elseif (preg_match('/[\@\_\!\#\$\%\^\&\*\(\)\<\>\?\/\\\|\}\{\~\:\;\,]/', $wordTable)) {
        //case3
    } else {
        //undefined
    }
}

The meaning of the regex is:

  • [a-zA-Z]{3}$: Out of a set of small and capital letters, there must be three, at the end ($) of the word.
  • [\d]{3}$: Out of a set of digits, there must be three, at the end ($) of the word.
  • Mind that all regex start and end with a delimiter character, I used the slash.
  • Special characters must be escaped with backslash. I didn't want to check which ones you need to escape, so I escaped them all.
Zsolt Szilagyi
  • 4,741
  • 4
  • 28
  • 44
0

Here's how I would implement it:

<?php
$words = 'The red fox eats meat, drinks and sleeps for 1000 hours';

function checkType($word) {
    if (preg_match("~\d{3}$~", $word)) {
        return ["digits", $word];
    } elseif (preg_match("~[a-zA-Z0-9]{3}$~", $word)) {
        return ["alnum", $word];
    } elseif (preg_match("~(?=.*\W)~", $word)) {
        return ["special", $word];
    } else {
        return ["unknown", $word];
    }
}

$tokens =  array_map ("checkType", explode(" ", $words));
print_r($tokens);
?>

This yields

Array
(
    [0] => Array
        (
            [0] => alnum
            [1] => The
        )

    [1] => Array
        (
            [0] => alnum
            [1] => red
        )

    [2] => Array
        (
            [0] => alnum
            [1] => fox
        )

    [3] => Array
        (
            [0] => alnum
            [1] => eats
        )

    [4] => Array
        (
            [0] => special
            [1] => meat,
        )

    [5] => Array
        (
            [0] => alnum
            [1] => drinks
        )

    [6] => Array
        (
            [0] => alnum
            [1] => and
        )

    [7] => Array
        (
            [0] => alnum
            [1] => sleeps
        )

    [8] => Array
        (
            [0] => alnum
            [1] => for
        )

    [9] => Array
        (
            [0] => digits
            [1] => 1000
        )

    [10] => Array
        (
            [0] => alnum
            [1] => hours
        )

)
Jan
  • 42,290
  • 8
  • 54
  • 79
0

If it can help . The following logic satisfies both sentences:

The red fox eats7 meat,drinks and sleeps for 1000 hours

The red fox eats meat, drinks and sleeps for 1000 hours

<?php
$words = 'The red fox eats7 meat,drinks and sleeps for 1000 hours';
$words=explode(' ', $words);//seperate the words and assign to an array

echo "<h1>Your Phrase has been Parsed</h1>"; ?>
<table>
    <thead>
       <td>Word</td>
       <td>Length</td>
       <td>Type</td>
    </thead>
        <tbody>
          <?php foreach($words as $word){ ?>
                <tr>
                    <td><?php echo $word; ?></td>
                    <td><?php echo strlen($word);  ?></td>
                    <td>
                    <?php 
                    if (preg_match("/[`'\"~!@# $*()<>,:;{}\|]/", $word)){
                      echo "word has a special character";
                    } else {
                      if(is_numeric(substr($word, -3)))
                      echo "Ends with 3 digits";
                      else if (ctype_alpha(substr($word, -3)))
                      echo "Ends with 3 alphabetic characters";
                      else
                      echo "Undefined type";
                    }
                    ?>
                    </td>
                </tr>
          <?php } ?>
        </tbody>
</table>
my_workbench
  • 300
  • 3
  • 8
  • I tried this phrase: 'The red fox eats7 meat,drinks and sleeps for 1000 hours' which must parse the words as follows: eats7 as undefined type meat,drinks as ends with 3 alphabets meat,drinks as has special characters But I am getting: eats7 as ends with 3 alphabets meat,drinks as ends with 3 alphabets – kingkwabs Aug 10 '20 at 17:05
  • for `eats7` does it work ? if not, what is the required output ? – my_workbench Aug 10 '20 at 17:07
  • eats7 must be undefined as it ends with alphanumeric characters. – kingkwabs Aug 10 '20 at 17:12
  • awesome! - Is there a way I can filter the table with a button click and remove example - all words with alphabets and leave the rest? – kingkwabs Aug 11 '20 at 09:53
  • @AlfredOfosu good to hear. it helped. For filter, use jquery something like this should help: https://www.w3schools.com/jquery/jquery_filters.asp – my_workbench Aug 11 '20 at 09:56