Jump to content

glinky

New Members
  • Posts

    9
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

glinky's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Hello, I´m making a script to search a database for a certain word and I´m using this query: $query = "SELECT * FROM contents WHERE text LIKE '%$searchDb%'"; It works fine but I would like to know if there is a way to find out the exact word it founds. For example if I search for "econom" it will return a text where exists the word "economy". Is there a way to know the word it based its search on? In this case a way to return the value "economy"? Is there a way of doing this? (sorry if this is a stupid question!) Help apreciated Thanks
  2. Hello, I´m making a script to search a database for a certain word and I´m using this query: $query = "SELECT * FROM contents WHERE text LIKE '%$searchDb%'"; It works fine but I would like to know if there is a way to find out the exact word it founds. For example if I search for "econom" it will return a text where exists the word economy. Is there a way to know the word it based its search on? I mean...without using lots of array explodes and stuff like that... Is there a simple elegant way of doing this? (sorry if this is a stupid question!) Help apreciated Thanks
  3. 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!
  4. 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']; } }
  5. I tried that before and nothing...
  6. I echo the query and it looks the fine... SELECT * FROM conteudos WHERE titulo like '%two clocks%' I really can´t understand this...
  7. Correction ...sorry There was and extra "OR" that doesnt make sense... Query: $search = cleanSpaces($get); $query = "SELECT * FROM conteudos WHERE titulo like '%$search%'";
  8. 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%'";
  9. 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
×
×
  • 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.