putnamehere
03-13-2006, 05:02 AM
I'm creating a website search script and it's nearly complete but thers just one problem.
First of all let me show you my code for the seach script.
Ther are 2 main files s.php and id.txt ( btw it's flat file )
id.txt is set out like this.
WORD-OR-COMMENT-TO-SEARCH|result / link to word seached.
heres my exact database for it.
home|Your shearch for the word home has got this result
tutorials|Your shearch for the word tutorials has got this result
php|Your shearch for the word php has got this result
website|Your shearch for the word guestbook has got this
1234|Your shearch for the word 1234 has got this result
Right now the php.
<?
$userlist = array();
$handle = fopen("id.txt", 'rb');
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096); // read the file one line at a time
$buffer = trim($buffer);
$userlist[] = $buffer;
}
fclose($handle);
}
// you can ignor that top part it's here what needs a fix.
$str = $_POST['s'];
$str = strtolower($str);
foreach ($userlist as $line) {
$x = explode('|', $line);
if ( $str == $x[0] ) {
echo "$x[1] ";
}
}
?>
ok well it works thers no errors or anyting.
I have set up a live demo at www.phpcodes.co.uk/s/ go to it and try searching for "php" or try typeing in "tutorials".
However go ther and type in "php tutorials" and you get nothing.
I knwo why it is, it's because i'v set it to look for the exact word and that word isent in teh database i'v looked into useing preg match.
So istead of
if ( $str == $x[0] )
it was
if (preg_match("$str", "$x[0]"))
But i got this error
Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in C:\Program Files\Abyss Web Server\htdocs\down\loginf.php on line 20
help?
First of all let me show you my code for the seach script.
Ther are 2 main files s.php and id.txt ( btw it's flat file )
id.txt is set out like this.
WORD-OR-COMMENT-TO-SEARCH|result / link to word seached.
heres my exact database for it.
home|Your shearch for the word home has got this result
tutorials|Your shearch for the word tutorials has got this result
php|Your shearch for the word php has got this result
website|Your shearch for the word guestbook has got this
1234|Your shearch for the word 1234 has got this result
Right now the php.
<?
$userlist = array();
$handle = fopen("id.txt", 'rb');
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096); // read the file one line at a time
$buffer = trim($buffer);
$userlist[] = $buffer;
}
fclose($handle);
}
// you can ignor that top part it's here what needs a fix.
$str = $_POST['s'];
$str = strtolower($str);
foreach ($userlist as $line) {
$x = explode('|', $line);
if ( $str == $x[0] ) {
echo "$x[1] ";
}
}
?>
ok well it works thers no errors or anyting.
I have set up a live demo at www.phpcodes.co.uk/s/ go to it and try searching for "php" or try typeing in "tutorials".
However go ther and type in "php tutorials" and you get nothing.
I knwo why it is, it's because i'v set it to look for the exact word and that word isent in teh database i'v looked into useing preg match.
So istead of
if ( $str == $x[0] )
it was
if (preg_match("$str", "$x[0]"))
But i got this error
Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in C:\Program Files\Abyss Web Server\htdocs\down\loginf.php on line 20
help?