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

Link to comment
Share on other sites

[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]
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.