= 1)) { //echo substr_count($search,' '); //echo '
'.$search.'
'; $search = str_replace(' ',' ',$search); } //remove multiple speech marks: while ((substr_count($search,'""') >= 1)) { $search = str_replace('""','"',$search); } //define globals so they can be returned by functions $GLOBALS['highlight1'] = ''; $GLOBALS['highlight2'] = ''; $GLOBALS['result'] = ''; function debug($lineno) { echo 'PHP line no: '.$lineno.''; } function test($variable,$lineno) { echo '

PHP line no: '.$lineno.' TEST VARIABLE = '.$variable.'

'; } function highlight($colloop,$thefield,$thesearch) { $fieldlength = strlen($thefield); $searchlength = strlen($thesearch); $pos = strpos($thefield, $thesearch); $stringend = $searchlength + $pos; $slice1 = substr($colloop, 0, $pos); $slice2 = substr($colloop, $pos, $searchlength); $slice3 = substr($colloop, $stringend); $GLOBALS['result'] = $slice1.$GLOBALS['highlight1'].$slice2.$GLOBALS['highlight2'].$slice3; //rebuild text with highlight // echo '
'.$GLOBALS['result']; } $title = 'Flat File Database Demo 5 Results'; echo << $title

$title

HTML; if (($search == '') or ($search == '"')) {exit('No usable search string found!');} $fp = fopen('ffdd5.txt','r'); if (!$fp) {exit('

ERROR: Unable to open database file!

');} $row = 0; while (!feof($fp)) { $line = fgets($fp,1024); //use 2048 if very long lines $col[$row] = ($row + 1).'|'.$line; $row++; } fclose($fp); // Avoid sort below if you have numbers at the start of each line! //It puts all the lines beginning with 1 first, eg: 1, 10, 11, 12 etc. //sort($col); reset($col); $arrays = count($col) - 1; $tablecells = ''; $loop = -1; $and = $once = $found = $found1 = $found2 = $bothfound = $search2also = 'no'; $count = 0; $speechmarks = ''; //test for AND but not two words that must be together (""): if ((strstr($search,' AND ')) and ($search[0] <> '"')) { $and = 'yes'; list ($word1, $word2, $word3) = explode (' ', $search); //remove AND $word1 = strtolower($word1); $word3 = strtolower($word3); if ($word1 == $word3) { $search = $word1; $and = 'no'; } else { $search1 = $word1; $search2 = $word3; while ($loop < $arrays) { $loop++; $field = strtolower($col[$loop]); if ((strstr($field,$search1)) and (strstr($field,$search2))) { $found = $found1 = $found2 = $bothfound = 'yes'; //word1 highlight($col[$loop],$field,$search1); $col[$loop] = $GLOBALS['result']; //word2 $field = strtolower($col[$loop]); //update field to include highlighted version from above highlight($col[$loop],$field,$search2); //add rows $col[$loop] = explode('|',$GLOBALS['result']); $tablecells.=' '.$col[$loop][0].' '.$col[$loop][1].' '.$col[$loop][2].' '.$col[$loop][3].' '.$col[$loop][4].' '.$col[$loop][5].' '; $count++; } } } } if (($and == 'no') and ($bothfound == 'no')) { $search = strtolower($search); $search1 = $search2 = ''; if (strstr($search,' ')) { list ($search1, $search2) = explode (' ', $search); if ($search2 == $search1) {//when both words are the same $search = $search1; //make search just first word $search1 = $search2 = ''; //destroy both words from split } } //test for space in search but not two words that must be together (""): if ((strstr($search,' ')) and ($search[0] <> '"')) { list ($search1, $search2) = explode (' ', $search); //search for two separate words while ($loop < $arrays) { $loop++; $search2also = 'no'; $field = strtolower($col[$loop]); $fieldkept = $field; //preserve text without web code if (strstr($field,$search1)) { if (strstr($field,$search2)) {$search2also = $bothfound = 'yes';} $found = $found1 = 'yes'; highlight($col[$loop],$field,$search1); $col[$loop] = $GLOBALS['result']; if ($search2also <> 'yes') { //only output once when both words on same row //add rows $col[$loop] = explode('|',$GLOBALS['result']); $tablecells.=' '.$col[$loop][0].' '.$col[$loop][1].' '.$col[$loop][2].' '.$col[$loop][3].' '.$col[$loop][4].' '.$col[$loop][5].' '; $count++; } } $field = strtolower($col[$loop]); //update field to include highlighted version from above if (strstr($fieldkept,$search2)) { //search on line without highlighting added $found = $found2 = 'yes'; highlight($col[$loop],$field,$search2); if (($search2also == 'yes') or ($found2 == 'yes')) { //add rows $col[$loop] = explode('|',$GLOBALS['result']); $tablecells.=' '.$col[$loop][0].' '.$col[$loop][1].' '.$col[$loop][2].' '.$col[$loop][3].' '.$col[$loop][4].' '.$col[$loop][5].' '; $count++; } } } } else { //check if speech marks and remove them: if ($search[0] == '"') { $search = str_replace('"','',$search); $speechmarks = '\''; //for output later } if ($search <> '') { //do normal search while ($loop < $arrays) { $loop++; $field = strtolower($col[$loop]); if (strstr($field,$search)) { $found = $found1 = 'yes'; highlight($col[$loop],$field,$search); //add rows $col[$loop] = explode('|',$GLOBALS['result']); $tablecells.=' '.$col[$loop][0].' '.$col[$loop][1].' '.$col[$loop][2].' '.$col[$loop][3].' '.$col[$loop][4].' '.$col[$loop][5].' '; $count++; } } } else {exit('No usable search string found!');} } } //if ($and == 'no') if ($found == 'yes') { echo '

Your search for \''.$speechmarks.$search.$speechmarks.'\' was found in the following '.$count.' lines of the database:

'.$tablecells.'
Doctor Who Series 5, 2010
Episode Writer Director Producer Aired On
'; } else { echo '

Your search for \''.$speechmarks.$search.$speechmarks.'\' was not found.

'; } echo '

By Christopher Hester 3rd May 2010. Last updated: '.date('jS F Y', getlastmod()).' · About The Demo · Comments

'; ?>