Page 1 of 1

help needed with a search engine!

Posted: Sun Jun 05, 2005 11:31 am
by kickmaster
hi everyone!
i am new here!

well my problem is as follows:
i wrote a search engine, and it works ok.
the next thing i need to do is to get the results and to show them with the page's title and not its name.suffix

example - now it shows me this: index.php

i want it to show this:

Portals - home page
(that is the title of the page)

how do i put a variable into the <title>text</title> of a page?

if anyone knows please Email me or contact me via messenger (same address)

guytzvia@netvision.net.il

thank you!

Posted: Sun Jun 05, 2005 11:51 am
by shiznatix
well most people will just reply to this post on this thread so thats what im going to do.

Code: Select all

<title>Page Number <? echo $page_number; ?></title>
bam if your on page number 3 and your variable $page_number is the page number you are on it will display as the title Page Number 3. Im sure you get the idea.

Posted: Sun Jun 05, 2005 11:51 am
by Roja
The reason you came here was because the forums are loaded with helpful people.

The forums are loaded with helpful people because they found helpful answers - to questions like yours - here.

Please don't ask us to answer questions elsewhere (via email, or IM) - it reduces the values of the forums - which means you won't want to come here for help anymore.

You can even set your preferences in your profile to email you when someone replies to your post.. allowing us to use the forums as they were meant to be, allowing you to get the answers you want, and allows you to be notified by email.

----

As to your question, you can parse the page itself, and get the title tag of the page out. If you control the content, you could even store the title tag in the database along with the list of pages. Then its a simple matter of displaying the title with the result.

It entirely depends on how you have it setup.

sorry - will do

Posted: Sun Jun 05, 2005 12:29 pm
by kickmaster
sorry will do from now on!

Posted: Sun Jun 05, 2005 12:32 pm
by kickmaster

Code: Select all

<title>Page Number <? echo $page_number; ?></title>
I'm sorry, but i don't realy get it!

$page_number takes the information from where?
can it be a regular string and not an integer?
can you explain it a bit further?

thannk you!

Posted: Sun Jun 05, 2005 12:41 pm
by Roja
kickmaster wrote: $page_number takes the information from where?
Depends on where you have it.

Do you have a database with the filenames and contents listed, or are you processing the files during each search?

Obviously, the page name comes from the page, but if you already parse the page (probably so, for a search engine), or if you store keywords for each file in a database along with the filename, you could be storing the page title as well..

It just depends on how you are doing it. Explain what your search engine does, and we can be more specific.
kickmaster wrote:can it be a regular string and not an integer?
Yes. It can be anything. The code snippet for your problem, as stated, would be:

Code: Select all

$pagename = &quote;Portals - home page&quote;;
...
echo &quote;Result: &quote; . $pagename;
Of course, the first line, setting pagename, will come from another source (file, database, etc).

Posted: Sun Jun 05, 2005 12:45 pm
by kickmaster
this is how my engine looks like

link (page in hebrew - search for the word EBAY...(capital letters...)

http://www.gnm.co.il/search.php

and if you can find why i get the results twice it would be nice to! :oops:

Code: Select all

<form action=&quote;&quote; method=&quote;get&quote;>
						    <div align=&quote;right&quote;>
						      <p>
						      <input type=&quote;text&quote; name=&quote;find&quote;>
						      <br>
                              <input type=&quote;submit&quote;  value=&quote;&#1513;&#1500;&#1495; &quote;name=&quote;ok&quote;>
						      </p>
						      <p>
						        <?
						  global $text;
						  $str=$_GET&#1111;&quote;find&quote;];
						  $l=strlen($str);
						  if((substr($str,1,1)==&quote;&quote;) and (substr($str,$l-1,1)==&quote;&quote;))
						  {
						  $str=substr($str,2,$l);
						  $l=strlen($str);
						  $text=$str;
						  find();
						  }
						  else
						  {
						  $mess=explode(&quote; &quote;,$str);
						  $n=sizeof($mess);
							//echo $n.&quote;--&quote;;
						  for($i=0;$i<$n;$i++)
						  {
						  $text=$mess&#1111;$i];
						  $l=strlen($text);
						  if($l!=0)
						  find();
						  }
						  }
						  function find()
						  {
						  global $text;
						  //$text=strtolower($text);
						  //echo $text.&quote;<br>&quote;;
						  $i=0;
						  $j=2;
						  $dirname=&quote;.&quote;;
						  $dh=opendir($dirname);
						  $file=readdir($dh);
						  while ($file !=false)
						  {
						  $path = $dirname.&quote;/&quote;.$file;
						  if(is_dir($path))
						  {
						  $names&#1111;$i]=$path;
						  $i=$i+1;
						  }
						  if(is_file($path))
						  {
						  $fp=fopen($path,&quote;r&quote;);
						  while(!feof($fp))
						  {
						  $line=fgets($fp,1024);
						  if(strstr($line,$text))
						  echo &quote;<a href=&quote;.$file.&quote;>&#1500;&#1495;&#1510;&#1493; &#1499;&#1488;&#1503; &#1500;&#1502;&#1506;&#1489;&#1512; &#1500;&#1491;&#1507; &#1492;&#1489;&#1488;</a><br>&quote;;
						  }
						  }
						  $file=readdir($dh);
						  }
						  closedir($dh);
						  }s
						  ?>                        
						                  </p>
						    </div>
						  </form>

Posted: Sun Jun 05, 2005 12:52 pm
by Roja
Okay, so you process the file on each search.

You'd need to watch for the title tag, and pull the contents of it.

You could do it something like (this isn't working code):

Code: Select all

$line=fgets($fp,1024);
if(strstr($line,$text))
{
    if(strstr($line,"<title>"))
    {
        // cut $line from the start of "<title>" to the end of "</title>"
        // Using strpos and substr.
        $pagetitle = substr...
    }

    echo "<a href=".$file.">" . $pagetitle . "</a><br>";
}

Posted: Sun Jun 05, 2005 12:59 pm
by kickmaster
I'm sorry if I am a pain but am am kind of new to this...

what is the rest of the :

$pagetitle = substr...

:oops: :oops:

Posted: Sun Jun 05, 2005 1:03 pm
by Roja
kickmaster wrote:I'm sorry if I am a pain but am am kink of new to this...

what is the rest of the :

$pagetitle = substr...

:oops: :oops:
Look up substr and strpos in the fine manual.. they are fairly straightforward:

http://us4.php.net/manual/en/function.substr.php
http://us4.php.net/manual/en/function.strpos.php

Use strpos to get the starting location ("offset") of the title tag, and then use substr to cut from that offset to the end of the /title tag.

Posted: Sun Jun 05, 2005 1:33 pm
by Burrito
you might be better served doing it with regex

ex:

Code: Select all

<?
$string = "<title>Hello There</title>";
$pattern = "#<title>(.*?)</title>#i";
preg_match($pattern,$string,$matches);
echo $matches[1];
?>

Posted: Sun Jun 05, 2005 2:22 pm
by Ambush Commander
By the way, everyone's been giving examples with <?, but it's much better to use <?php rather than <?