Page 1 of 1

code help !! string replace of %s output in a search script

Posted: Sun Oct 19, 2003 4:07 am
by sona
this may be asking for too much but still ....

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.';
}
?>
foruma/fr3/messages = where the searching is done
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

Posted: Sun Oct 19, 2003 6:33 am
by markl999
This simplest way is just to convert the results returned into the format you want rather than trying to decipher that, rather ugly, script you got from the cookbook ;)
Try replacing

Code: Select all

foreach ($matching_pages as $k => $v) { 
print sprintf('<li> <a href="%s">%s</a>',$v[0],$v[1]); 
}
with this

Code: Select all

foreach ($matching_pages as $k => $v) {
    $id = end(explode('/', $v[0]));
    $url = $_SERVER['DOCUMENT_ROOT'].'/index.php?subject=view&msg='.$id;
    echo '<li> <a href="'.$url.'">'.$url.'</a>';
}
This is just off the top of my head so it's not gauranteed to work first time :o

Posted: Sun Oct 19, 2003 8:59 am
by sona
hi markl999 - you are an angel. thanks a million !! i cannot pay you right now but may be someday small gift or some stuff ....

thanks again !!

Posted: Sun Oct 19, 2003 9:01 am
by markl999
ooh, it worked then? I didn't think it would first time...that's a first ;)

Posted: Sun Oct 19, 2003 9:20 am
by sona
i was going thur ur site ..very nice design and *helpful* contents

i liked the news script - particularly becoz it allows easy pagination and very simple - i was unable to find any link as to what newscript it is - then thought it must be dev by you - congrats. if it is open source and downloadable ?may be post a link in hotscripts

thanks again

Posted: Sun Oct 19, 2003 9:27 am
by markl999
Actually i'm always lazy when it comes to my own sites, so i used XOOPS (no comments please ;))
http://www.xoops.org

Posted: Sun Oct 19, 2003 11:09 am
by sona
ok - i saw the xoops site and sites using xoops may be 2 or 3 months ago , but urs seems neater
anyway -- thanks again . :)

Posted: Mon Oct 20, 2003 12:38 am
by sona
sorry for being greedy :(

markl999 if you are here theres 1 more thing ....

previously becoz of that sprint f %s
we cud get the results as a list of titles ( what the script found within <title> tags )

is there any way to get back that while keeping the same pattern of urls ?

thnx again

Posted: Mon Oct 20, 2003 12:44 am
by markl999
Try

Code: Select all

echo '<li> <a href="'.$url.'">'.$v[1].'</a>';
8O

Posted: Mon Oct 20, 2003 6:13 pm
by sona
you are a gem !! thanks a million, thanks a trillion :)