#native_company# #native_desc#

Use of implode() and explode() in formatting text/keywords used in search

By koree
on March 23, 2004

This script uses explode() and implode() to separate and combine elements for arrays.

About the searching of words you could always use and sql statement to search for a word/phrase and later use explode() and implode() to parse and format words in the result text.


$letters = "a b c";
$letters = explode(" ",$letters);

the result of the commands above would be an array named $letters[i] >>

$letters[0] = a
$letters[1]= b
$letters[2]= c

Implode() works in a similar way…

implode(separator, $variable-array);

$letters = implode(" ", $letters);
echo "letters = ".$letters;

REMEMBER that $letters is already an array because of the explode() function.

the result of the command above is:

letters = a b c

here’s the script…

//use this script to format the keyword(s) in your search

//per word
$quote = "a quick brown fox jumped over the lazy dog";
$explode = explode(" ", $quote);
$count = count($explode);
echo "$count = number of words
"; echo "$explode[3] = explode elem #3
"; $bold_txt = "jumpeD"; for($i=0; $i<=($count-1); $i++){ if (strtoupper($explode[$i]) == strtoupper($bold_txt)) { $explode[$i] = " ".$explode[$i]." "; } } $implode = implode(" ", $explode); echo "implode = $implode

"; //per phrase $phrase = "quick brown + lazy dog";//search term $phrase2 = str_replace("+", " ", $phrase); $explode_phrase = explode(" ", $phrase2);//separate by array $count3 = count($explode_phrase);//count number of elements in an array $quote2 = "a quick brown fox jumped over the lazy dog"; $explode2 = explode(" ",$quote2); $count2 = count($explode2); for ($a=0;$a<=($count2-1);$a++) { for ($c=0;$c<=($count3-1);$c++) { //first elem of $explode_phrase_words to a matching word on the $quote2 text if(strtoupper($explode_phrase[$c]) == strtoupper($explode2[$a])){ $explode2[$a] = " ".$explode2[$a]." "; } } } echo "searterm : $phrase
"; $implode2 = implode(" ", $explode2); echo "implode = $implode2

"; echo "ksm2004"; ?>