theonewhotopes Posted June 7, 2007 Share Posted June 7, 2007 Hey all, I'm trying to validate user input in a search field on my website, and my regex works great, except for the fact that no matter what I try, I can't get it to recognize apostrophes. I even tried the following regex in a regex editor, and it matched words with apostrophes, such as "O'malley". I am completely lost as to how I should proceed. Here's my code: <? $var = @$_GET['q']; $trimmed = trim($var); //trim whitespace from the stored variable if (!preg_match("/^[0-9a-zA-Z0-9À-ÖØ-öø-ÿ \'.-]{3,40}$/", $trimmed)) $trimmed = ""; $strlen = strlen($trimmed); if ($trimmed == "") { echo "<p>Please enter a search...</p>"; } // check for a search parameter elseif (!isset($var)) { echo "<p>We dont seem to have a search parameter!</p>"; } else{ include("db.php"); $trimmed = mysql_real_escape_string($trimmed); $query = "select id FROM info WHERE name LIKE \"%$trimmed%\" $result = mysql_query($query); $numrows = mysql_num_rows($result); if ($numrows == 0) { echo "<h4>Results</h4>"; echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>"; } // display what the person searched for echo "<p>You searched for: "" . $trimmed . ""</p>"; // begin to show results set echo "Results <br>"; //display results code goes here ?> <form name="form" action="search.php" method="get"> <input type="text" name="q" size="15" maxlength="40"> <input type="submit" name="Submit" value="Search"> </form> Whenever I enter a search that contains an apostrophe, it just displays "Please enter a search...". Again, I am completely lost here, so any help at all would be greatly appreciated. Thanks! Quote Link to comment Share on other sites More sharing options...
leap500 Posted June 7, 2007 Share Posted June 7, 2007 Hi You may want to give this a go: if (!preg_match("/^[0-9a-zA-Z0-9À-ÖØ-öø-ÿ \'.-]*$/", $trimmed)) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.