Forum Moderators: coopster

Message Too Old, No Replies

PHP4: Simple internal search engine question

I'm trying to implement a little SE that searches only the current page

         

jd80

7:20 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



I am trying to program an internal search engine that works only on a given page.
What I want is to dynamically generate links to the "Sections" that contain the
search term. Here is the simple document:

<html>
<head><title>SearchTestA1.html</title></head>

<body>
<div id="Title of Section 1(endTitle)">
<p>Paragraph 1 in section 1 searchTerm</p>
<p>Paragrpah 2 in section 1</p>
</div>
<div id="Title of Section 2(endTitle)">
<p>Paragraph 1 in section 2</p>
<p>Paragrpah 2 in section 2 searchTerm</p>
</div>
<form action="GetTitles.php" method="post">
<input type="text" value="searchTerm" name="searchTerm" />
<input type="submit"/>
</form>
</body>
</html>

Now here is the php code that attempts to extract the "Titles" (i.e., the id
attributes of the divisions) that will eventually be wrapped in a link:

<?php #GetTitles.php
$htmlDoc = file_get_contents("SearchTestA1.html");
$htmlDoc = htmlentities($htmlDoc);
$var1 = <see notes below...>;
$beginChunks = explode($var1, $htmlDoc); // an array in which each element begins with "id="
$endChunks = explode("(endTitle)", $htmlDoc); // an array in which each element begins with
// "(endTitle)"
$offset = 0; // an offset in necessary for the following functions
// so that the position of the elements aren't duplicated

// store the begining positions of the "Titles" in an array
for($i = 0; $i < count($beginChunks); $i++){
$titleBeginPos[$i] = strpos($htmlDoc, $beginChunks[$i], $offset);
$offset += 10;
}

$offset = 0; // reset the offset

// store the positions of the ends of the "Titles" in an array
for($i = 0; $i < count($endChunks); $i++){
$titleEndPos[$i] = strpos($htmlDoc, $endChunks[$i], $offset);
$offset += 10;
}

// store the titles in an array
for($i = 1; $i < count($beginChunks); $i++){
$titles[$i-1] = substr($htmlDoc, $titleBeginPos[$i] + 6, ($titleEndPos[$i] - $titleBeginPos[$i]) - 16) . "<br/>";
}

// print the titles
for($i = 0; $i < count($titles); $i++){
echo $titles[$i];
}
?> // I stop here b/c this is all the code needed to ask my question

________________________________________________________________________________________
QUESTION:
How come if $var1 = "<div id="; then the only thing printed is:

0

But when $var1 = "id="; then the following gets printed:

Title of Section 1
Title of Section 2

Many thanks! 8)

John

coopster

12:43 am on Mar 2, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Since you have converted all applicable characters to HTML entities you need to change your search variable accordingly:
$var1 = '&lt;div id=';

The htmlentities function is identical to htmlspecialchars() [php.net] in all ways and if you look at the manual pages for htmlspecialchars you will notice it states:

'<' (less than) becomes '&lt;'