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

  • Home
  • SEARCH
  • 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 3219808
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:41:20+00:00 2026-05-17T15:41:20+00:00

what would be the best way to write a code in Php that would

  • 0

what would be the best way to write a code in Php that would search within a webpage for a number of words stored in a file? is it best to store the source code in a file or is it another way? please help.

  • 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-17T15:41:21+00:00Added an answer on May 17, 2026 at 3:41 pm

    The best way is to use google: site:example.com word1 OR word2 OR word3

    Do you want to search in ONE PAGE? or one website with MULTIPLE PAGES?
    If its only one page i think you can store the html code in memory without problems.
    if you know exactly what you search strpos for reach word will probably be the fastest (stripos for case insensitive). you can also define your own character class and use preg_match_all or something… just something like this will do…

    <?
    $keywords = array("word1","word2","word3");
    $doc = strip_tags(file_get_contents("http://www.example.com")); // remove tags to get only text
    $doc = preg_replace('/\s+/', ' ',$doc); // remove multiple whitespaces...
    foreach($keywords as $word) {
      $pos = stripos($doc,$word);
      if($pos !== false) {
        echo "match: ...".str_replace($word,"<em>$word</em>",substr($doc,$pos-20,50))."... \n";
      }
    }
    ?>
    

    something like the following for example will perform MUCH faster as its based on hashmap lookups with O(1) and doesnt need to scan the whole text for every keyword…

    <?
    setlocale(LC_ALL, "en_US.utf8");
    $keywords = array("word1","word2","word3","word4");
    $doc = file_get_contents("http://www.example.com");
    $doc = strtolower($doc);
    $doc = preg_replace('!/\*.*?\*/!s', '', $doc);
    $doc = preg_replace("/<!--.*>/i", "", $doc);
    $doc = preg_replace('!<script.*?script>!s', '', $doc);
    $doc = preg_replace('!<style.*?style>!s', '', $doc);
    $doc = strip_tags($doc);
    $doc = preg_replace('/[^0-9a-z\s]/','',$doc);
    $doc = iconv('UTF-8', 'ASCII//TRANSLIT', $doc); // check if encoding is really utf8
    //$doc = preg_replace('{(.)\1+}','$1',$doc); remove duplicate chars ... possible step to add even more fuzzyness
    $doc = preg_split("/\s+/",trim($doc));
    foreach($keywords as $word) {
      $word = strtolower($word); 
      $word = iconv('UTF-8', 'ASCII//TRANSLIT', $word);
      $key = array_search($word,$doc);
      var_dump($key);
      if($key !== false) {
        echo "match: ";
        for($i=$key;$i<=5 && isset($doc[$i]);$i++) {
          echo $doc[$i]." ";
        }
      }
    } 
    ?>
    

    this code is untested.

    it would be however be more elegant to dump textnodes from a domdocument

    Simple searching is easy. If you want to search in a whole website the crawling logic is difficult.

    I once did a backlink-checker for a company that worked like a crawler.

    My first advice is not to do a recursion (like scanning a page and following all links and following all links in that until you reach a certain level…)

    rather do it like this:

    • do a for loop as often as many levels you want to crawl.

    • set a site array with one entry (start page)

    • pass array to a function downloads every link, scans the site there and stores links on it in array.
      when done with all links return the new link list array

    • in the for loop update the array with the return value of the function, and call the function again.

    this way you can avoid following nasty paths but rather crawl website level by level.
    also store already visited links in an array to skip, dont follow external links, check for weird url parameters etc..

    for future use you can store documents in lucene or solr, there are classes to turn html pages into senseful lucene objects and search within.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was told that $.getJSON is the best way to send data to and
I am trying to figure out the best way to name my HTML form
I have a website that was launched a few months ago. I would like
I know I know, a database would be the better idea with what I
I'm developing an object-oriented PHP website right now and am trying to determine the
Overview: We are looking to write a C# user interface to select parts of
I know ASP.NET C# very well. I make a lot of stuff like surveys
I'm doing a bit of coursework for university and I'm completely baffled. Basically I've
I'm creating a new web application (Rails 3 beta), of which pieces of it
I'm trying to get my head round Unit Testing and there's one more piece

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.