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 9219585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:08:58+00:00 2026-06-18T03:08:58+00:00

I am currently using PHPCrawler for some search functionality on a site. I need

  • 0

I am currently using PHPCrawler for some search functionality on a site. I need to remove some of the page elements from being indexed.

For example, I have used:

$doc_body = preg_replace('/<li>(.*?)<\/li>/is', "", $doc_body);

to remove lists, because I don’t want the lists in the results. This works exactly as it should.

Now, another thing I need to remove is the following:

<div class="example">all contents within</div>

so for this I have tried:

   $doc_body = preg_replace('/<div(.*?)class="(.*?)example(.*?)"(.*?)>(.*?)<\/div>/is', "", $doc_body);

Which produces an error because perhaps not every page has the div class example.
So I have adapted it with the following code:

      if(strpos($doc_body,'<div class="example">')){
      $doc_body = preg_replace('/<div(.*?)class="(.*?)example(.*?)"(.*?)>(.*?)<\/div>/is', "", $doc_body);
      }

That unfortunately does not work either! It doesn’t produce an error, but it doesn’t remove and all contents from the results.

This is my first time working with either phpcrawler or Domdocument…although I am not sure if my problem here has anything to do with them?

  • 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-06-18T03:08:59+00:00Added an answer on June 18, 2026 at 3:08 am

    I’d suggest you take a look at DOMDocument and XPath which is used to query the document model much like CSS does, but with a bit different syntax. W3Schools have a lightweight tutorial on XPath here.

    Regular expressions is always a bad idea when parsing an entire document since it is both resource heavy and time consuming.

    E.g, to find every div with the class “example” using XPath, you’d just query the document as such;

    //div[@class="example"]
    

    Then remove the nodes with the DOMDocument api and finally normalize, in order to get the final result.

    $doc = new DOMDocument();
    $xpath = new DOMXPath($doc);
    $doc->loadHTML($html);
    
    // Remove all the lists
    foreach ($xpath->query("//ul | //ol") as $node) {
         $node->parentNode->removeChild($node);
    }
    
    // Remove all <div class="example" /> nodes
    foreach ($xpath->query("//div[@class='example']") as $node) {
         $node->parentNode->removeChild($node);
    }
    
    $doc->normalize();
    
    // Get the final document for indexing
    $html = $doc->saveHTML();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im currently using the JQuery UI extension for its search autocomplete. However, I need
iam currently using NSURLConnection class to hit my server and get some data from
I currently using android NDK to write some native code in C. I have
so currently using ajax with JQuery .post to return a form populate with some
Currently using friendly_id on my messages table. Messages are accessed like this: site.com/messages/id e.g.
Im currently using PHP to fetch results from a mysql db. Im also displaying
Im currently using C# to pass some objects to powershell and executing a method
We're currently using JUnit 4.4 and Ant 1.7.1 for our builds. I have some
Currently using wxHTML to display a remote web page in a C++ Windows wxWidgets
Currently using a really simple Twisted NameVirtualHost coupled with some JSON config files to

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.