hello....

i am doing search concept. i am searching one word from all my web pages .


actually i have only one table for 25 static pages. among this pages i want to search searching content. and also highlight the search content. how to do?

Dani AI

Generated

asked for word highlighting across pages; pointed at a client-side (JS) solution, posted a PHP example using the deprecated ereg family, and correctly warned to avoid ereg and use the PCRE functions instead. Below are practical, safe options you can apply depending on whether your stored content is plain text or HTML.

For plain-text fields (fast and simple), escape the user term and wrap matches with a span using preg_replace. This is fine when your column stores only text (no tags). Always use preg_quote to escape the search term before building the pattern, and use the i flag for case-insensitive matching.

For HTML content (more common for page bodies), do not run regex on the entire HTML string — that can break tags or highlight inside attributes. Instead parse the fragment and only change text nodes. The PHP DOM approach below finds text nodes, splits matches, and replaces them with text + <span class="highlight">...</span> nodes so the markup stays valid.

function highlight_html($html, $term) {
  $doc = new DOMDocument();
  @$doc->loadHTML('<?xml encoding="utf-8" ?>' . $html);
  $xpath = new DOMXPath($doc);
  $nodes = $xpath->query('//text()[not(ancestor::script) and not(ancestor::style) and normalize-space()]');

  $pat = '/(' . preg_quote($term, '/') . ')/i';
  foreach ($nodes as $n) {
    if (preg_match($pat, $n->nodeValue)) {
      $frag = $doc->createDocumentFragment();
      $parts = preg_split($pat, $n->nodeValue, -1, PREG_SPLIT_DELIM_CAPTURE);
      foreach ($parts as $i => $part) {
        if ($i % 2 === 1) {
          $span = $doc->createElement('span', $part);
          $span->setAttribute('class', 'highlight');
          $frag->appendChild($span);
        } else {
          $frag->appendChild($doc->createTextNode($part));
        }
      }
      $n->parentNode->replaceChild($frag, $n);
    }
  }
  return $doc->saveHTML();
}

A client-side alternative gives instant feedback without a reload. Consider the lightweight library mark.js to avoid reimplementing edge cases. Style the highlight with CSS:

.highlight { background: yellow; color: inherit; }

Notes: always sanitize user input (for regex and XSS), limit highlight to reasonable lengths to avoid DoS, and for searching many pages do full-text search (DB index or a search engine) and highlight only on the matched pages. For PHP DOM reference see DOMDocument.

Recommended Answers

All 5 Replies

I dont know how to make the function, i would like it so that when the user enters the text it searchs the text on the page and hilites behind the text yellow. So its a bit like a "find" function that hilites behind the word that it is asked to find.

i want only how to highlight bit of word in that pharagraph.

Don't know if I understood well, but I guess you need a JavaScript if your highlight should happen dynamically (on the same time person enters the search word). If so, try to Google for JavaScript text highlighting.

I dont know how to make the function, i would like it so that when the user enters the text it searchs the text on the page and hilites behind the text yellow. So its a bit like a "find" function that hilites behind the word that it is asked to find.

i want only how to highlight bit of word in that pharagraph.

try this

function search_highlight($needle, $replace, $haystack)
{
 $haystack = eregi_replace($needle,$replace,$haystack);
 return $haystack;
}
echo search_highlight($searchtext, "<b><font style='color:white; background-color:grey;'>" . $searchtext . "</font></b>", total description);

try this

function search_highlight($needle, $replace, $haystack)
{
 $haystack = eregi_replace($needle,$replace,$haystack);
 return $haystack;
}
echo search_highlight($searchtext, "<b><font style='color:white; background-color:grey;'>" . $searchtext . "</font></b>", total description);

thank you pushpa......i got it.

try this

function search_highlight($needle, $replace, $haystack)
{
 $haystack = eregi_replace($needle,$replace,$haystack);
 return $haystack;
}
echo search_highlight($searchtext, "<b><font style='color:white; background-color:grey;'>" . $searchtext . "</font></b>", total description);

DON'T use ereg* functions. There's a reason why there are GIANT RED WARNINGS at the top of every page of the PHP documentation with these functions in them. Use preg_*

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.