in the main folder mydomain.com, the folder foruma/fr3/messages contains some numbered.php files ( eg 1000.php, 1001.php so on )
i wanted to add a search function so that a word submitted for search can be found out from these files.
however for this files to get displayed in browser they need url like
/foruma/fr3/index.php?subject=view&msg=1001.php
[ http://localhost/foruma/fr3/index.php?s ... g=1001.php ]
*instead of*
/foruma/fr3/messages/4209.php
http://localhost/foruma/fr3/messages/4209.php
The search function which I got from php cookbook will return results like
/foruma/fr3/messages/1001.php
/foruma/fr3/messages/1002.php
/foruma/fr3/messages/1003.php
but *what i need* is
results ( taking those numbers only )
/foruma/fr3/index.php?subject=view&msg=1001.php
/foruma/fr3/index.php?subject=view&msg=1002.php
/foruma/fr3/index.php?subject=view&msg=1003.php
i am failing miserably - what will be the actual tweaking in the code - i will be full of extreme gratitude if that help is possible.
---------------
the search code
---------------
Code: Select all
<?
function pc_search_dir($dir) {
global $body_regex,$title_regex,$seen;
$pages = array();
$dirs = array();
$seen[realpath($dir)] = true;
if (is_readable($dir) && ($d = dir($dir))) {
while (false !== ($f = $d->read())) {
$path = $d->path.'/'.$f;
if (is_file($path) && is_readable($path)) {
$realpath = realpath($path);
if ($seen[$realpath]) {
continue;
} else {
$seen[$realpath] = true;
}
$file = join('',file($path));
if (preg_match($body_regex,$file)) {
$uri = substr_replace($path,'',0,strlen($_SERVER['DOCUMENT_ROOT']));
if (preg_match('#<title>(.*?)</title>#Sis',$file,$match)) {
array_push($pages,array($uri,$match[1]));
} else {
array_push($pages,array($uri,$uri));
}
}
} else {
if (is_dir($path) && ('.' != $f) && ('..' != $f)) {
array_push($dirs,$path);
}
}
}
$d->close();
}
foreach ($dirs as $subdir) {
$realdir = realpath($subdir);
if (! $seen[$realdir]) {
$seen[$realdir] = true;
$pages = array_merge($pages,pc_search_dir($subdir));
}
}
return $pages;
}
function pc_page_sort($a,$b) {
if ($a[1] == $b[1]) {
return strcmp($a[0],$b[0]);
} else {
return ($a[1] > $b[1]);
}
}
$matching_pages = array();
$seen = array();
$search_dirs = array('foruma/fr3/messages');
$body_regex = '#name(.*' . preg_quote(search_item,'#').
'.*)displayreply#Sis';
foreach ($search_dirs as $dir) {
$matching_pages = array_merge($matching_pages,
pc_search_dir($_SERVER['DOCUMENT_ROOT'].'/'.$dir));}
if (count($matching_pages)) {
usort($matching_pages,'pc_page_sort');
print '<ul>';
foreach ($matching_pages as $k => $v) {
print sprintf('<li> <a href="%s">%s</a>',$v[0],$v[1]);
}
print '</ul>';
} else {
print 'No pages found.';
}
?>search_item = the term submittable via a search form
%s = this thing needs to be modified so that only the number is taken and the urls are reconstructed as , for example,
/foruma/fr3/index.php?subject=view&msg=xxxx.php
[http://localhost//foruma/fr3/index.php? ... g=xxxx.php]
Once again , thanks a lot for reading this.
Regards