explode into array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mcralle
Forum Newbie
Posts: 2
Joined: Fri Jun 04, 2010 6:20 am

explode into array

Post by mcralle »

Hi,

I'm trying to make a search engine using full-text in boolean mode. I am using following code to hightlight the search results:

Code: Select all

$searchterm2=stripslashes($searchterm);

function highlightWords($text, $words, $colors=null)
{

        if(is_null($colors) || !is_array($colors))
        {
       	$colors = array('yellow', 'pink', 'green');
        }

        $i = 0;
        /*** the maximum key number ***/
        $num_colors = max(array_keys($colors));

        /*** loop of the array of words ***/

	foreach ($words as $word)
        {
                
/*** quote the text for regex ***/
                $word = preg_quote($word);
                
                /*** highlight the words ***/
                $text = preg_replace("/\b($word)\b/i", '<span class="highlight_'.$colors[$i].'">\1</span>', $text);
                if($i==$num_colors){ $i = 0; } else { $i++; }
        }
        /*** return the text ***/
        return $text;


  }  

and the following to show the results

while($row=$result->fetchRow()){

$words= array(preg_replace('/["]/','',$searchterm2));

$kilde = $row['kilde'];
$kilde = highlightWords($kilde, $words);

$graesk = $row['graesk'];
$graesk = highlightWords($graesk, $words);

$dansk = $row['dansk'];
$dansk = highlightWords($dansk, $words);

$engelsk = $row['engelsk'];
$engelsk = highlightWords($engelsk, $words);




echo '
<table style="width: 800px"><tr>	
<td style="width: 80px" valign="top" class="rowcontainer2"><p>'.$kilde.'</p></td>
<td style="width: 200px" valign="top" class="rowcontainer2"><p>'.$graesk.'</p></td>
<td style="width: 200px" valign="top" class="rowcontainer2"><p>'.$dansk.'</p></td>
<td style="width: 200px" valign="top" class="rowcontainer2"><p>'.$engelsk.'</p></td>
</tr>';

}
}

I've been trying to use explode() to split the $words= array(preg_replace('/["]/','',$searchterm2)); in witch way I would see the results in different colours, but so far I've haven't got it to work. It works if I just fill ind the words manually, like : $words= array(And, Cyrus, gave, him)

Could anybody tell me how to fix that, please? Help would be much appreciated...
Last edited by Benjamin on Sun Jun 06, 2010 3:23 am, edited 1 time in total.
Reason: Added [syntax=php] tags.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: explode into array

Post by Jonah Bron »

Try echoing the value of $words immediately after definition. That will give us some debugging information.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: explode into array

Post by AbraCadaver »

I'm not following. You need to give an example of what the variables contain and what you want the result to be.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
mcralle
Forum Newbie
Posts: 2
Joined: Fri Jun 04, 2010 6:20 am

Re: explode into array

Post by mcralle »

Thank you for takign the time...

I've made the webpage http://www.paradeigmatolexikon.dk, whitch gives examples when you search a word.

What I would like is first of all the serchterms to be highlighted, when I search for more than one word. Second, whitch not so urgent, is the searched word or sentence to stay in the searchbox, when quoted "".

I just give you the whole code:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />


<title>PARADEIGMATOLEXIKON</title>

<link href="default.css" rel="stylesheet" type="text/css" media="screen" />

<script language="javascript" type="text/javascript">

window.onload=function(){

if(document.getElementById&&document.

getElementsByTagName&&document.createElement){

var sfield=document.getElementsByTagName('form')[0].elements[0];

//if(!sfield){return};

//sfield.onfocus=function(){this.value=''};

//sfield.onblur=function(){

//if(!this.value){this.value='Søg og du skal finde!'};


//}

}

}

</script>

</head>

</html>

Code: Select all

<?php

// include MySQL-processing classes

require_once 'mysql.php'; 

try{

// connect to MySQL

$db=new MySQL(array('host'=>'www.paradeigmatolexikon.dk','user'=>'user','password'=>'password','database'=>'database'));

$searchterm=$db->escapeString($_GET['searchterm']);



$searchterm2=stripslashes($searchterm);




$db->query("set names utf8");


?>

Code: Select all

<body style="margin-top: 5px; margin-bottom: 5px">

<h1>PARADEIGMATOLEXIKON</h1>

<div class="maincontainer">


<form method="get" action="processform.php" class="style4">

<input type="text" name="searchterm" title="Søg og du skal finde!" value="<?php echo $searchterm2; ?>"
class="searchbox" />

<input type="submit" name="search" title="Search Now!
"value="Søg" class="searchbutton" />

</form>

</div>

</body>

Code: Select all

<?php


$result=$db->query("SELECT * FROM anabasis WHERE MATCH (kilde, graesk, dansk, engelsk) AGAINST ('$searchterm' IN BOOLEAN MODE) ORDER BY RAND()");


if (!$result->countRows()){
$result=$db->query("SELECT * 
FROM `anabasis` 
WHERE `Kilde` REGEXP '\[[:space:]]$searchterm\[ .,;:?]'
OR `Graesk` LIKE '% $searchterm%'
OR `Dansk` REGEXP '\[[:space:]]$searchterm\[ .,;:?]'
OR `Engelsk` REGEXP '\[[:space:]]$searchterm\[ .,;:?]' ORDER BY RAND()");

}
if (!$result->countRows()){


echo '<br><div class="maincontainer"><h2 style="margin-bottom: 1px; margin-top: 1px;">'.$searchterm2.' GAV INGEN RESULTATER. PRØV IGEN!</h2></div>';


}


elseif($result->countRows()==1){



    function highlightWords($text, $words, $colors=null)
{

        if(is_null($colors) || !is_array($colors))
        {
       	$colors = array('yellow', 'pink', 'green');
        }

        $i = 0;
        /*** the maximum key number ***/
        $num_colors = max(array_keys($colors));

        /*** loop of the array of words ***/

	foreach ($words as $word)
        {
                
/*** quote the text for regex ***/
                $word = preg_quote($word);
                
                /*** highlight the words ***/
                $text = preg_replace("/\b($word)\b/i", '<span class="highlight_'.$colors[$i].'">\1</span>', $text);
                if($i==$num_colors){ $i = 0; } else { $i++; }
        }
        /*** return the text ***/
        return $text;


  }  




echo '<br><div class="maincontainer"><p><strong>DU FIK '.$result->countRows().' RESULTATER</p></strong>
<table style="width: 800px"><tr>
		<td style="width: 72px" class="rowcontainer"><h3>KILDE</td>
		<td style="width: 200px" class="rowcontainer"><h3>GRÆSK</td>
		<td style="width: 200px" class="rowcontainer"><h3>DANSK</td>
		<td style="width: 200px" class="rowcontainer"><h3>ENGELSK</td>
		</tr>';




while($row=$result->fetchRow()){

$words= array(preg_replace('/["]/','',$searchterm2));

$kilde = $row['kilde'];
$kilde = highlightWords($kilde, $words);

$graesk = $row['graesk'];
$graesk = highlightWords($graesk, $words);

$dansk = $row['dansk'];
$dansk = highlightWords($dansk, $words);

$engelsk = $row['engelsk'];
$engelsk = highlightWords($engelsk, $words);




echo '
<table style="width: 800px"><tr>	
<td style="width: 80px" valign="top" class="rowcontainer2"><p>'.$kilde.'</p></td>
<td style="width: 200px" valign="top" class="rowcontainer2"><p>'.$graesk.'</p></td>
<td style="width: 200px" valign="top" class="rowcontainer2"><p>'.$dansk.'</p></td>
<td style="width: 200px" valign="top" class="rowcontainer2"><p>'.$engelsk.'</p></td>
</tr>';

}
}

else{


function highlightWords($text, $words, $colors=null)
{

        if(is_null($colors) || !is_array($colors))
        {
       	$colors = array('yellow', 'pink', 'green');
        }

        $i = 0;
        /*** the maximum key number ***/
        $num_colors = max(array_keys($colors));

        /*** loop of the array of words ***/

	foreach ($words as $word)
        {
                
/*** quote the text for regex ***/
                $word = preg_quote($word);
                
                /*** highlight the words ***/
                $text = preg_replace("/\b($word)\b/i", '<span class="highlight_'.$colors[$i].'">\1</span>', $text);
                if($i==$num_colors){ $i = 0; } else { $i++; }
        }
        /*** return the text ***/
        return $text;


  }  


    
echo '<br><div class="maincontainer"><p><strong>DU FIK '.$result->countRows().' RESULTATER</p></strong>
<table style="width: 800px"><tr>
		<td style="width: 72px" class="rowcontainer"><h3>KILDE</td>
		<td style="width: 200px" class="rowcontainer"><h3>GRÆSK</td>
		<td style="width: 200px" class="rowcontainer"><h3>DANSK</td>
		<td style="width: 200px" class="rowcontainer"><h3>ENGELSK</td>
		</tr>';




while($row=$result->fetchRow()){

$words = array(preg_replace('/["]/','',$searchterm2));

$kilde = $row['kilde'];
$kilde = highlightWords($kilde, $words);

$graesk = $row['graesk'];
$graesk = highlightWords($graesk, $words);

$dansk = $row['dansk'];
$dansk = highlightWords($dansk, $words);

$engelsk = $row['engelsk'];
$engelsk = highlightWords($engelsk, $words);




echo '
<table style="width: 800px"><tr>	
<td style="width: 80px" valign="top" class="rowcontainer2"><p>'.$kilde.'</p></td>
<td style="width: 200px" valign="top" class="rowcontainer2"><p>'.$graesk.'</p></td>
<td style="width: 200px" valign="top" class="rowcontainer2"><p>'.$dansk.'</p></td>
<td style="width: 200px" valign="top" class="rowcontainer2"><p>'.$engelsk.'</p></td>
</tr>'; 



}

}

echo '</div></table>';

}

catch(Exception $e){

echo $e->getMessage();

exit();

}


?>
Last edited by Benjamin on Sun Jun 06, 2010 3:25 am, edited 1 time in total.
Reason: Added [syntax=php] tags.
Post Reply