Jump to content

extract only words from search string (for security)?


alpineedge3

Recommended Posts

i have a php script that searches a mysql database using terms from a user input box.  i want to have a script that will extract "words" (or alphanumeric groupings) from the string and put them into an array. i don't want to create the array based on a string separated by spaces (' ') because that is not secure.

what's the best way to do this? thanks

[code]
<?php
$string = <<<STR
one fish two fish red fish blue fish.
word1 word2 word3.
#$#%^A b&*.- +1230
STR;

preg_match_all('/\b[a-z0-9]+\b/i', $string, $array);
echo '<pre>', print_r($array, true), '</pre>';

?>
[/code]

Outputs:

[tt]
Array
(
    [0] => Array
        (
            [0] => one
            [1] => fish
            [2] => two
            [3] => fish
            [4] => red
            [5] => fish
            [6] => blue
            [7] => fish
            [8] => word1
            [9] => word2
            [10] => word3
            [11] => A
            [12] => b
            [13] => 1230
        )

)
[/tt]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.