In my opinion, you should use the code in post #7, with attribution in an HTML comment so it shows in the page source. If you want to include a URL in the comment, just point it tenfingers dot net/tools/atoalpha/. This is my working version. The one in post #7, is wpwversion.php at the same URL.
Assuming you are going to be expanding the site to include other widgets (which this example could be known as) then keeping the PHP resources separate from HTML pages and templates so they can be re-used by all the documents on your site, not just the one page... would be good idea.
The PHP above, would fall into the category, 'methods and modules' as there is no page generation involved, only data crunching or retrieval. The 'methods' (script procedures) include the functions along with the procedure starting with $var=. The 'modules' (what I call them) are the predefined array, $STACK, and, $NPD, defined as the string constant, "No POST data". Both of these are known as 'statements' in PHP. Since they have no closure (they are not contained within a function) their scope is global, meaning any function or method can call upon them. In our example, we can see that the main method is also without closure, so it too executes within the same scope as our global CONSTANTS and the statements that defined them. Anyway, this is getting away from things...
index.php
PHP Code:
<?php require_once "library.inc"; ?>
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Multiword Phrase NATO Phonetic Alphabet converter</title>
<meta name="description" content="A PHP method to convert alphabet letters to their NATO phonetic callout">
<style>textarea{width:400px;height:300px}</style></head><body>
<form method="post" action="index.php">
<input type="text" name="text" autofocus="autofocus" size="50">
<input type="submit" value="Send"><br>
<textarea disabled="disabled"><?php echo "Sent: $sentChar\nResult: $charResult\n"; ?></textarea>
<textarea disabled="disabled"><?php echo "Sent: $sent\n>> $charResults\n"; ?></textarea>
</form></body></html>
library.inc
PHP Code:
<?php /* Multiword Phrase NATO Phonetic Converter by RC Pierce 2012-04-08 */
$STACK=array('A'=>'Alpha','B'=>'Bravo','C'=>'Charlie','D'=>'Delta','E'=>'Echo','F'=>'Foxtrot','G'=>'Golf','H'=>'Hotel','I'=>'India','J'=>'Juliet','K'=>'Kilo','L'=>'Lima','M'=>'Mike','N'=>'November','O'=>'October','P'=>'Papa','Q'=>'Quebec','R'=>'Romeo','S'=>'Sierra','T'=>'Tango','U'=>'Uniform','V'=>'Victor','W'=>'Whiskey','X'=>'X-ray','Y'=>'Yankee','Z'=>'Zulu');
$NPD = "No POST data";
function postFilter($post) {
$post = preg_replace("/^[ ]+|[^a-z ]|^[ ]+$/i", "", $post);
if (!$post) return NULL;
return strtoupper($post);
};
function postSplit($post) {
$postChars = array();
$postChars = preg_split('//', $post, -1, PREG_SPLIT_NO_EMPTY);
return $postChars;
};
function alphaToPhonetic($post) {
global $STACK;
foreach ($STACK as $alpha => $phonetic) { if ($post == $alpha) return $phonetic; };
};
function wordToPhonetic($posts) {
$result = "";
foreach ($posts as $index => $letter) { $result .= "\n" . alphaToPhonetic($letter); };
return $result;
};
$var = ($_POST['text']) ? $_POST['text'] : NULL;
if ($var) {
$sent = postFilter($var);
if ($sent) {
$sentChar = substr($sent, 0, 1);
if ($sentChar) { $charResult = alphaToPhonetic($sentChar); } else { $charResult = $NPD; };
if (strlen($sent) > 1) { $charResults = wordToPhonetic(postSplit($sent)); } else { $charResults = $charResult; };
} else { $sent = $sentChar = $NPD; };
} else { $sent = $sentChar = $NPD; };
?>
Be sure to remember that the main method is without closure, and will execute every time this resource is called. To control 'when' it runs requires giving it closure within a function, or only including the resource when it is needed. For now, we are fine, but if you begin adding modules to this library, things might have to change.
Why .inc? Again, arbitrary. I use .inc to signify m&m's. They are includes, so .inc works fine for me. The truth is it doesn't matter what you use for an extension on a PHP include file, as long as it's not an already defined extension or MIME type that the server has built in methods for handling. Unless configured, .htm, .html, among others, will not be parsed for PHP by the server.
Notice the 'require_once' statement, above? This is how we attach the PHP to the HTML document. Now you may wish to keep your library out of the way, where it's safe. You can do this by creating a folder called 'includes' (again, arbitrary) and store the file in there. Your statement would then read, 'require_once "includes/library.inc";'
I won't get into now, but you should know there are some real gotchas when traversing directories in PHP. Everything is happening in real time on the server, and if includes contain references to other resources, the location of the include file is focal to how the path is constructed pointing to those resources. Read up on getcwd() and other topics relating to Current Working Directory. Our example will work if you store it away in a folder, so not to worry. This would be the recommended approach.
Last thing of note to this post... The markup is HTML5. As you can see, it is still HTML, just slimmer, and a lot more semantic. In our example there is nothing really that sets it apart from a normal HTML page, so I'll list the few things that are different: No type attribute on STYLE or SCRIPT; No long doctype; no Content-type or Language. I use HTML 4 markup, not XHTML, but both are valid, so pick one and stick with it or your pages will look in disarray.