Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6755749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:26:24+00:00 2026-05-26T13:26:24+00:00

I use the below function found in Highlight keywords in a paragraph for highlighting

  • 0

I use the below function found in Highlight keywords in a paragraph for highlighting keywords in a string. Thus it generates this warning:

Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: htmlParseEntityRef: expecting ';' in Entity, line: 1 in /../ on line 118

Following this thread Warning: DOMDocument::loadHTML(): htmlParseEntityRef: expecting ';' in Entity, the answers suggests using html entitiy encoding but doing that misses the whole purpose of using DOM to search through the string and highlighting without breaking the tags. E.g. a htmlentities and html_entity_decode would highlight alla occurences.

How should I tackle this? Or is there some other problem with the function that I am missing?

function highlight($string,$query){
    $keywords = explode(" ",$query);
    //define
    $keywordsCIS = array();
    foreach($keywords as $value){
        $lcValue = strtolower($value);
        $keywordsCIS[] = $value;
        $keywordsCIS[] = $lcValue;
        $keywordsCIS[] = ucfirst($lcValue);
        $keywordsCIS[] = strtoupper($lcValue);
    }
    $dom = new DomDocument();
    $dom ->recover = true;
    $dom -> strictErrorChecking = false;
    $dom -> loadHtml($string);
    $xpath = new DomXpath($dom);
    foreach ($keywordsCIS as $keyword) {
        $elements = $xpath->query('//*[contains(.,"' . $keyword . '")]');
        foreach ($elements as $element) {
            foreach ($element->childNodes as $child) {
                if (!$child instanceof DomText) continue;
                $fragment = $dom->createDocumentFragment();
                $text = $child->textContent;
                $stubs = array();
                while (($pos = stripos($text, $keyword)) !== false) {
                    $fragment->appendChild(new DomText(substr($text, 0, $pos)));
                    $word = substr($text, $pos, strlen($keyword));
                    $highlight = $dom->createElement('strong');
                    $highlight->appendChild(new DomText($word));
                    $highlight->setAttribute('class', 'kw');
                    $fragment->appendChild($highlight);
                    $text = substr($text, $pos + strlen($keyword));
                }
                if (!empty($text)) $fragment->appendChild(new DomText($text));
                $element->replaceChild($fragment, $child);
            }
        }
    }
    //$string = $dom->saveXml($dom->getElementsByTagName('body')->item(0)->firstChild);
    $string = $dom->saveHTML();
    return $string;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:26:24+00:00Added an answer on May 26, 2026 at 1:26 pm

    I believe the warning you are getting is from the html the DomDocument is attempting to parse. I assume that you do not actually wish to change the html content in $string that is being parsed, prior to parsing.

    Try using the @ operator on the loadHTML line to avoid the warning:

    @$dom->loadHtml($string);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I typically use the below function to return the root URL if I ever
I use the line below in my C# winform app, this works great but
In the code below I use mpf_add to add the string representation of two
In this website I've found a function to convert seconds in an human readable
Can anyone explain this strange behavior found when trying to use sapply and which.min
Anyone know why this keeps returning a blank image? I found this function here
Below is a function I found about a year ago that is supposed to
I found the function below : CFStringCapitalize Changes the first character in each word
I found a function to convert seconds to HH:MM:SS online, the function is this:
How to Remove Badge from the tabbar item i had use below code but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.