Add Searching The Contents Blog Post Contents

By Default, Search in HTMLy only queries the filename.

I have added code that enables the Search function to capture whole-word results from the *contents* of each blog post.

See:

https://splifingate.net/blog/

vs

https://splifingate/test/blog/

My Study:

https://andyrew.info/blog/2025/01/searching-inside-htmly-content
This commit is contained in:
splifingate 2025-01-26 14:35:35 -05:00
commit 9ac3fc114a
2 changed files with 32 additions and 18 deletions

View file

@ -1323,13 +1323,18 @@ function get_keyword($keyword, $page, $perpage)
$tmp = array();
$words = explode(' ', $keyword);
foreach ($posts as $index => $v) {
$arr = explode('_', $v['basename']);
$filter = $arr[1] . ' ' . $arr[2];
foreach ($words as $word) {
if (stripos($filter, $word) !== false) {
$filepath = $v['dirname'] . '/' . $v['basename'];
$findRxWhole = '\b' . preg_quote($keyword, '~') . '\b'; // Add word boundaries (find whole-words only)
$findRx = "~{$findRxWhole}~iu"; // Case-insensitive and UTF-8 mode
$lines = file($filepath);
foreach ($lines as $line) {
if (preg_match ($findRx, $line)) {
if (!in_array($v, $tmp)) {
$tmp[] = $v;
}
@ -1505,23 +1510,30 @@ function get_tagcount($var)
return count($tmp);
}
// Return search result count
/// Return search result count
function keyword_count($keyword)
{
$posts = get_blog_posts();
$tmp = array();
$words = explode(' ', $keyword);
foreach ($posts as $index => $v) {
$arr = explode('_', $v['basename']);
$filter = $arr[1] . ' ' . $arr[2];
foreach ($words as $word) {
if (stripos($filter, $word) !== false) {
$tmp[] = $v;
}
}
$filepath = $v['dirname'] . '/' . $v['basename'];
$findRxWhole = '\b' . preg_quote($keyword, '~') . '\b'; // Add word boundaries (find whole-words only)
$findRx = "~{$findRxWhole}~iu"; // Case-insensitive and UTF-8 mode
$lines = file($filepath);
foreach ($lines as $line) {
if (preg_match ($findRx, $line)) {
if (!in_array($v, $tmp)) {
$tmp[] = $v;
}
}
}
}
$tmp = array_unique($tmp, SORT_REGULAR);

View file

@ -28,9 +28,11 @@
<?php endif;?>
<?php if (isset($is_search)):?>
<div class="row justify-content-center" style="padding-top: 4rem;">
<!-- main.html.php -->
<div class="row justify-content-center" style="padding-top: 3rem;">
<div class="col-md-12 text-center">
<h1 class="mt-0"><?php echo $search->title;?></h1>
<h2 class="mt-0">Search: <span style='color: #628B48;'><?php echo $search->title;?></span> (<?php echo $search->count;?>)</h2>
<form><input type="search" name="search" class="form-control is-search" placeholder="<?php echo i18n('Type_to_search');?>"></form>
</div>
</div>
<?php endif;?>