0

I want to produce search result all letters that is caps or small,but now it's displaying exact keywords result only,so that i changed my original code $pattern = "/".$searchTerm."([a-zA-Z0-9])*/"; to like this $pattern = "/".$searchTerm."([a-zA-Za-ZA-z0-9])*/";

but this display some errors and output not coming

Error :

Warning: preg_match_all(): Compilation failed: range out of order in character class at offset 14 in /var/app/current/WS/search.php on line 30

Warning: Invalid argument supplied for foreach() in /var/app/current/WS/search.php on line 31 {"message":"success","response":1,"result":[]}

My search code :

if($searchTerm&&$searchType)
    {
        if($searchType=='hash') {
            $query = "SELECT hashTag FROM `".$table."` WHERE hashTag LIKE '%#".$searchTerm."%'";
            $stmt = $database->prepare($query);
            // $stmt->bind_param('s',$searchTerm);
            $stmt->execute();    
            $stmt->store_result();

            if($stmt->num_rows>0) {
                $tempArray = array();
                $stmt->bind_result($hashTag);

                while ($row =$stmt->fetch()) {
                    $pattern = "/".$searchTerm."([a-zA-Za-ZA-z0-9])*/";
                    preg_match_all($pattern,$hashTag,$matches);
                    foreach ($matches[0] as $key => $value) {
                        $finalArray[$value] = "";
                    }
                }

                foreach ($finalArray as $key => $value) {
                    $tempArray = array();
                    $tempArray['hash'] = $key ;
                    array_push($megaArray, $tempArray);
                }       

                $stmt->free_result();
                $stmt->close();
                $finalArray = array();
                $finalArray = $megaArray;

                $message = "success";
                $response = 1;
            } 
        }

What is the solution to produce the result to the search term is caps or small?

Barmar
  • 596,455
  • 48
  • 393
  • 495
  • The `a-Z` is not in order, because the uppercase `Z` preceds the lowercase `a` in codepoints. And you probably should utilize `preg_quote()` too. – mario Apr 09 '15 at 05:37
  • The original `a-zA-Z` should have gotten upper- and lower-case letters. Why did you think you needed to add `A-z` and `a-Z`? – Barmar Apr 09 '15 at 05:38
  • yaa my old code is $pattern = "/".$searchTerm."([a-zA-Z0-9])*/"; . but it is only exact result. see the DB stored like #varun,if i search #VARUN means i want that #varun result,but now i want #varun result means the search term need exactly #varun.This is my problem. –  Apr 10 '15 at 06:41
  • i solved this issue thank you guys for your kind replies, i changed my codes like this : if($searchType=='hash') { $query = "SELECT hashTag FROM `post` WHERE (hashTag like '%".$searchTerm."%')"; $stmt = $database->prepare($query); $stmt->execute(); $stmt->store_result(); if($stmt->num_rows>0) { $tempArray = array(); $stmt->bind_result($hashTag); while ($row =$stmt->fetch()) { $tempArray = array(); $tempArray['hashTag']= $hashTag; array_push($finalArray,$tempArray); } $stmt->free_result(); $stmt->close(); $message = "success"; $response = 1; } } Now it's working fine.once again tq guys –  Apr 10 '15 at 14:37

0 Answers0