Jump to content

Implode string problem


glinky

Recommended Posts

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

Link to comment
Share on other sites

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'];
		}
	}

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.