Jump to content

Check that word begins with specific letters


simonjk

Recommended Posts

Hi,

 

This is perhaps a bit of a simple one, but I am not sure whether it is a preg_match or strpos that I should use.

 

I have a form which is completed by the user. I want to check that in one of the form field entries the word they enter begins with E, L or B

 

I thought it might be built as a function, something like

function checkWord($word){

  return preg_match('/^E)(/^L)(/^B/'), $word) ? TRUE : FALSE;

 

Sorry if my attempt is laughable, can't seem to work this one out?

 

Any help appreciated.

 

Simon

Link to comment
Share on other sites

I would suggest adding strtoupper() to make the solution case insensitive:

 

<?php
function CheckWord($word)
{
    return in_array(strtoupper($word[0]), array('E', 'L', 'B'));
}

echo CheckWord('Ewok')   ? 'Yes' : 'No';    // Yes
echo CheckWord('word')   ? 'Yes' : 'No';    // No
echo CheckWord('enough') ? 'Yes' : 'No';    // Yes
?>

 

 

Unless you only want to match uppercase letters.

Link to comment
Share on other sites

Sorry guys, I perhaps should have been a little clearer with what I am trying to do.

 

The user will enter a code word ($icao) and this code is then to check that the word begins with an E, B or L.

 

I want to run the functions and if all is well post it to a variable.

 

This is the code I have (thanks to you). Does it look okay? I can't get it to work.

 

Thanks again,

Simon

 

 

 

function checkWord($icao)

    return in_array(strtoupper($icao[0]), array('E', 'L', 'B'), $ficao) ? TRUE : FALSE;

if(checkWord() != FALSE)

          {

        //If all is well we can  assign the value of POST field to a variable

                $userName = $_POST['userfcstarea'];

                }

        else

                {

        // if all is not well, we echo an error and exit the script

                echo 'Icao is invalid';

                exit();

                }

Link to comment
Share on other sites

Code should have been...

function checkWord($icao)

 

 

<code>

    return in_array(strtoupper($icao[0]), array('E', 'L', 'B'), $icao) ? TRUE : FALSE;

if(checkWord() != FALSE)

          {

        //If all is well we can  assign the value of POST field to a variable

                $userName = $_POST['userfcstarea'];

                }

        else

                {

        // if all is not well, we echo an error and exit the script

                echo 'Icao is invalid';

                exit();

                }

</code>

Link to comment
Share on other sites

function checkWord($icao)

    return in_array(strtoupper($icao[0]), array('E', 'L', 'B'), $ficao) ? TRUE : FALSE;

 

The body of a function definition needs to be enclosed in {..}s

in_array() returns true or false anyway so the ? TRUE : FALSE is redundant;

The third argument to in_array() is unnecessary (and if present should be true or false)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.