Jump to content


Photo

extract only words from search string (for security)?


  • Please log in to reply
1 reply to this topic

#1 alpineedge3

alpineedge3
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 28 July 2006 - 03:52 AM

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



#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 28 July 2006 - 05:06 AM

<?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>';

?>

Outputs:


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
        )

)

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users