user2740295
user2740295

Reputation: 31

Detect First Alphabetic Character

This is probably easier than I am making it out to be but I am looking to determine whether a entry from a user form contains a alphabetic character as the "first" letter only.

Example User Input:

<input type="text" name="faa_id" size="50" value="<?php echo $_POST['faa_id']?>" />

Example Form Processing

$faa_id = trim(strtoupper($_REQUEST['faa_id']));

if $faa_id = "string with any character except "N" in the FIRST position" {
   show URL-1; }

 else {
   show URL-2; }

Context: Data will be Aircraft Registration Numbers. Usually we enter them in our form without the default "N" (i.e. 345FR , not N345FR) but not all registration number start with N.

For example EJA345 is an aircraft callsign shown in place of generic N#, not registration number OR C-YEHE which is a Canadian Registration Number)

I know how to strip the first letter IF it were an "N" but I don't know how to detect if the first letters are alphabetic.

I suppose I would need to determine if first character is a alphabetic character first then check to see if it is a "N".

If not an "N", then URL 1 but if it is an "N" then URL 2

My code will hard code the URL similar to this:

Example URL-1 http://www.trackmyflight/N

Example URL-2 http://www.trackmyflight/

Upvotes: 2

Views: 290

Answers (4)

Matze
Matze

Reputation: 553

Try out regex, it was made for looking up strings for expressions (REGularEXpressions):

$pattern = '/^[nN].*$/';

You can check it on regexr.com. Just type a word which starts (or doesn't) with a N and put the pattern in the box after 'Expression'

In PHP you can check it with http://php.net/preg_match. The code should look like this:

$temp = preg_match($pattern, $faa_id)
if($temp === false)
{
   //If an error occurs
}
else
{
   if($temp == 0)
   {
     return URL1;
   }
   else
   {
     return URL2;
   }
}

Upvotes: 0

Gaurav Parashar
Gaurav Parashar

Reputation: 126

you can try this also

echo preg_match('/[A-Z-[N]]/',$faa_id[0] ); // true

Upvotes: 0

smistry
smistry

Reputation: 1126

You can use a regular expression:

$char = "A";

echo preg_match('/[A-Z]/',$char); // true

this checks if $char is equal to ONE captital letter.

Upvotes: 0

Fluffeh
Fluffeh

Reputation: 33532

You can treat the string as an array:

if ($faa_id[0] != "N")
{
    // do your show URL-1; 
}

Upvotes: 2

Related Questions