glinky Posted August 28, 2009 Share Posted August 28, 2009 Hi, I´m building a search module for a website and I wrote a function to take out undesirable extra spaces from a string. This way I will only get a single space between words. The function is this: function cleanSpaces($string){ $newString = explode (' ', $string); $array_dimension = count($newString); for ($i=0;$i<$array_dimension;$i){ if($newString[$i] == ' ' || $newString[$i+1] == ' '){ unset($newString[$i+1]); }else{ $i++; } } $string = implode(' ',$newString); return $string; } The problem is that after this function returns the new string (and it returns it right cause I tested) a query to the Database can´t no longer find the searched words. Example: If I search for "two clocks" the query to Db will find it and give me back the result. But if I search for "two clocks" my function turns it to "two clocks" but que query to Db is now empty. Is there some king of problem with the blank space enconding?? Apreciate some Help! Thanks Quote Link to comment Share on other sites More sharing options...
ReKoNiZe Posted August 28, 2009 Share Posted August 28, 2009 Your function looks fine, could you show us the code that does the query? Quote Link to comment Share on other sites More sharing options...
glinky Posted August 28, 2009 Author Share Posted August 28, 2009 Hi Thanks for qe quick answer! my query is this: $search = cleanSpaces($get); $query = "SELECT * FROM conteudos WHERE titulo like '%$search%' OR subtitulo LIKE '%$search%'"; Quote Link to comment Share on other sites More sharing options...
ReKoNiZe Posted August 28, 2009 Share Posted August 28, 2009 Your query looks fine, try making it echo your query so you can see if you have any weird spacing or anything. Quote Link to comment Share on other sites More sharing options...
glinky Posted August 28, 2009 Author Share Posted August 28, 2009 Correction ...sorry There was and extra "OR" that doesnt make sense... Query: $search = cleanSpaces($get); $query = "SELECT * FROM conteudos WHERE titulo like '%$search%'"; Quote Link to comment Share on other sites More sharing options...
glinky Posted August 28, 2009 Author Share Posted August 28, 2009 I echo the query and it looks the fine... SELECT * FROM conteudos WHERE titulo like '%two clocks%' I really can´t understand this... Quote Link to comment Share on other sites More sharing options...
ReKoNiZe Posted August 28, 2009 Share Posted August 28, 2009 Try changing it to WHERE titulo='$search' to see if that changes anything Quote Link to comment Share on other sites More sharing options...
glinky Posted August 28, 2009 Author Share Posted August 28, 2009 I tried that before and nothing... Quote Link to comment Share on other sites More sharing options...
ReKoNiZe Posted August 28, 2009 Share Posted August 28, 2009 Hmm.. Show us more code if you can then, sounds like it should work fine. Quote Link to comment Share on other sites More sharing options...
glinky Posted August 28, 2009 Author Share Posted August 28, 2009 Im not getting the searched value from a form because Im testing it so it comes from the var $get Theres also another function running but it´s after the query (as you can see ) so i soudn´t be because of that... $get = "two clocks"; $search = cleanSpaces($get); $searched = array(); $query = "SELECT * FROM conteudos WHERE titulo like '%$search%'"; echo $query; $result = mysql_query($query)or die(mysql_error()); while ($row = mysql_fetch_assoc($result)) { $titulo = $row['titulo']; $texto = $row['texto']; $words = LimpaString($titulo, $get); if ($words != null){ $titulo = highlightWords($titulo, $words); $texto = highlightWords($texto, $words); echo "<a href='search.php?section=".$row['section']."'>".$titulo."</a>"."<br>".$row['subtitulo']."<br><br>".$texto."<br><br><br>"; $searched[]=$row['id']; } } Quote Link to comment Share on other sites More sharing options...
ReKoNiZe Posted August 28, 2009 Share Posted August 28, 2009 That doesn't make. It should work, I'm not 100% why its not working. Your function must be adding some character we can't see. Quote Link to comment Share on other sites More sharing options...
glinky Posted August 28, 2009 Author Share Posted August 28, 2009 The problem ist´s actually in that other function... I´m sorry I´ve been wasting your time... I´m posting all the code including tha other function ... but I dont know if it is too big for you to take a look at ( I might think that because I´m not a PHP Pro). Anyway it worked before I used that cleaSpaces function...so I really can´t figure this out! mysql_select_db("search"); mysql_query("SET NAMES 'utf8'"); mysql_query('SET character_set_connection=utf8'); mysql_query('SET character_set_client=utf8'); mysql_query('SET character_set_results=utf8'); function LimpaString($string,$search, $enc = 'UTF-8'){ $acentos = array( 'A' => '/À|Á|Â|Ã|Ä|Å/', 'a' => '/à|á|â|ã|ä|å/', 'C' => '/Ç/', 'c' => '/ç/', 'E' => '/È|É|Ê|Ë/', 'e' => '/è|é|ê|ë/', 'I' => '/Ì|Í|Î|Ï/', 'i' => '/ì|í|î|ï/', 'N' => '/Ñ/', 'n' => '/ñ/', 'O' => '/Ò|Ó|Ô|Õ|Ö/', 'o' => '/ò|ó|ô|õ|ö/', 'U' => '/Ù|Ú|Û|Ü/', 'u' => '/ù|ú|û|ü/', 'Y' => '/Ý/', 'y' => '/ý|ÿ/', 'a.' => '/ª/', 'a.' => '/ª/', 'o.'=> '/º/' ); // Limpar o texto onde procurar $newString = preg_replace($acentos, array_keys($acentos), htmlentities($string,ENT_NOQUOTES, $enc)); $newString = ereg_replace("[^a-zA-Z0-9 ]", "", strtr($newString, "-", " ")); $newString = strtolower($newString); // Limpar o texto procurado $search = preg_replace($acentos, array_keys($acentos), htmlentities($search,ENT_NOQUOTES, $enc)); $search = ereg_replace("[^a-zA-Z0-9 ]", "", strtr($search, "-", " ")); $search = strtolower($search); //Separar os textos em palavras $palavrasNewStr = explode(' ',$newString); $palavrasStr = explode(' ',$string); $palavrasSearch = explode(' ',$search); //Contar palavras do texto procurado $searchLength = count($palavrasSearch); $words = array(); //Se a busca tiver mais que 1 palavra if($searchLength>1){ for( $i=0; $i < $searchLength; $i++ ){ $search = $palavrasSearch[$i]; $position = array_search( $search , $palavrasNewStr ); if($position != null){ $words[]=($search); }else{ $newStringLength = count($palavrasNewStr); for( $i=0; $i < $newStringLength; $i++){ $palavra = $palavrasNewStr[$i]; $letrasSearch = str_split($search); $letrasPalavra = str_split($palavra); $letrasCompare = array_intersect_assoc($letrasPalavra, $letrasSearch); $letrasIguais = count($letrasCompare); $numLetras = count($letrasPalavra); if( $letrasIguais >= ($numLetras)/2 && $numLetras > 2 ){ $words[$i] = $palavrasStr[$i]; //return $words; } }return $words; } }return $words; }else{ $position = array_search( $search , $palavrasNewStr ); if($position != null){ $palavra = $palavrasStr[$position]; $words[$i] = $palavra; return $words; }else{ $newStringLength = count($palavrasNewStr); for( $i=0; $i < $newStringLength; $i++){ $palavra = $palavrasNewStr[$i]; $letrasSearch = str_split($search); $letrasPalavra = str_split($palavra); $letrasCompare = array_intersect_assoc($letrasPalavra, $letrasSearch); $letrasIguais = count($letrasCompare); $numLetras = count($letrasPalavra); if( $letrasIguais >= ($numLetras)/2 && $numLetras > 2 ){ $words[$i] = $palavrasStr[$i]; //return "sdsdgsfg"; //return $words; } } return $words; } } } function cleanSpaces($string){ $newString = explode (' ', $string); $array_dimension = count($newString); for ($i=0;$i<$array_dimension;$i){ if($newString[$i] == ' ' || $newString[$i+1] == ' '){ unset($newString[$i+1]); }else{ $i++; } } $string = implode(' ',$newString); return $string; } function highlightWords($texto, $words){ foreach ( $words as $word ){ $texto = str_ireplace($word, '<span class="sublinhado">'.$word.'</span>', $texto); } return $texto; } $get = "jorge jesus"; $search = cleanSpaces($get); $searched = array(); $query = "SELECT * FROM conteudos WHERE titulo like '%$search%'"; echo $query; $result = mysql_query($query)or die(mysql_error()); while ($row = mysql_fetch_assoc($result)) { $titulo = $row['titulo']; $texto = $row['texto']; $words = LimpaString($titulo, $get); if ($words != null){ $titulo = highlightWords($titulo, $words); $texto = highlightWords($texto, $words); echo "<a href='search.php?section=".$row['section']."'>".$titulo."</a>"."<br>".$row['subtitulo']."<br><br>".$texto."<br><br><br>"; $searched[]=$row['id']; } } $query = "SELECT * FROM conteudos WHERE texto LIKE '%$search%'"; $result = mysql_query($query)or die(mysql_error()); while ($row = mysql_fetch_assoc($result)) { $id = $row['id']; if (in_array($id, $searched)){ return false; }else{ $texto = $row['texto']; $words = LimpaString($texto, $get); if ($words != null){ $texto = highlightWords($texto, $words); echo "<a href='search.php?section=".$row['section']."'>".$row['titulo']."</a>"."<br>".$row['subtitulo']."<br><br>".$texto."<br><br><br>"; } } } ?> Thanks once again! 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.