3
\$\begingroup\$

This is my first PHP script (although I like to think I'm a good general coder).

The script accepts a number from the user, uses it to generate a regular expression, and then returns all dictionary words that match that expression.

It's currently live here, and I'd appreciate any comments on style, security, best practices, and even efficiency. Please ignore the Html for now - it's got a WordPress template to be inserted into later on.

<html>
<head>
<title>Major Analysis Number Encoder</title>
</head>
<body>
    <H1>Tool for finding keywords for major analysis</h1>
    This tool uses the numbering system shown by Derren Brown in the
    excellent book
    <a
        href="http://www.amazon.co.uk/Tricks-Mind-Derren-Brown/dp/1905026269">Trick
        of the mind</a>. For a (much better) tool that uses the older
    numbering system see
    <a href="http://www.rememberg.com/">http://www.rememberg.com/</a><br>
    <form action="majorEncoder.php" method="post">
        <label for="firstname">Number to encode (at least three digits
            please):</label> <input type="text" id="targetNumber"
            name="targetNumber" /><br /> <input type="submit"
            value="Generate results" name="submit" />
    </form>
<?php
if ($_POST != null) {

    $target = $_POST ['targetNumber'];
    if (strlen ( $target ) >= 3) {
        $f = '[waeiouyc]?';
        $regex = '/^' . $f . $target;
        echo 'You entered the target' . $target . '<br><br>';
        $regex = str_replace ( '1', 'l' . $f, $regex );
        $regex = str_replace ( '2', 'n' . $f, $regex );
        $regex = str_replace ( '3', 'm' . $f, $regex );
        $regex = str_replace ( '4', 'r' . $f, $regex );
        $regex = str_replace ( '5', '[fv]' . $f, $regex );
        $regex = str_replace ( '6', '[bp]' . $f, $regex );
        $regex = str_replace ( '7', 't' . $f, $regex );
        $regex = str_replace ( '8', '[sc]h' . $f, $regex );
        $regex = str_replace ( '9', '[gd]' . $f, $regex );
        $regex = str_replace ( '0', '[sz]' . $f, $regex );
        $lines = file ( 'words.txt' );

        $fl_array = preg_grep ( $regex . '$/', $lines );
        if (sizeof ( $fl_array ) > 0) {
            echo 'Exact matches: <br>';
            foreach ( $fl_array as $element ) {
                echo $element . '<br>';
            }
            echo '<br>';
        }
        $fl_array = preg_grep ( $regex . '/', $lines );
        if (sizeof ( $fl_array ) > 0) {
            echo 'Matches that allow trailing characters: <br>';
            foreach ( $fl_array as $element ) {
                echo $element . '<br>';
            }
        } else {
            echo "No matches found for the input $target";
        }
    } else {
        echo 'Please enter at least three digits<br>';
    }
}
?>
</body>
</html>
\$\endgroup\$
1
\$\begingroup\$

One thing I would do is that str_replace can receive an array of items to look for and an array of its replacement:

PHP str_replace

// Provides: You should eat pizza, beer, and ice cream every day
$phrase  = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy   = array("pizza", "beer", "ice cream");

$newphrase = str_replace($healthy, $yummy, $phrase);

instead of going line by line to do it.

Just nit-picking but

sizeof() is an alias of count() and count() is more widely used, so use something that is more general in the PHP community

Also $_POST != null would typically be !isset($_POST) rather as explained in this post

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.