Jump to content

Help with search preg_match


theonewhotopes

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/54630-help-with-search-preg_match/
Share on other sites

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.