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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:42:39+00:00 2026-05-12T10:42:39+00:00

I have an HTML document as a string I want to search for a

  • 0

I have an HTML document as a string

I want to search for a keyword in this document and figure out where did it appear in the document

I mean in which tag did it appear

did it appear in H1,H2 or TITLE tag

lets say my document is

        $string = "<html>
                   <head> 
                   <title>bar , this is an example</title> 
                   </head> 
                   <body> 
                   <h1>latest news</h1>
                   foo <strong>bar</strong> 
                   </body>
                   </html>";


                   $arr = find_term("bar",$string);
                   print_r($arr);

I expect the result to be like this

                   [0]=> title
                   [1]=> strong

because “bar” appeared one time in TITLE tag and one time in the STRONG tag

I knew it is a complicated question, that is why I am asking if someone knows the answer 🙂

thanks

what I have so far is

        function find_term($term,$string){
               $arr = explode($term, $string);
               return $arr;
        }
        $arr = find_term("bar",$string);
        print_r($arr);

now we have an array which has the value

             Array
             (
             [0] => <html>
               <head>
               <title>

             [1] =>  , this is an example</title>
               </head>
               <body>
               <h1>latest news</h1>
               foo <strong>

             [2] => </strong>
               </body>
               </html>
             )

you can see that the last tag of every element of the array is the tag which contains “bar”
but the question now is how to know the last tag appeard in every element?

Thanks

  • 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-12T10:42:40+00:00Added an answer on May 12, 2026 at 10:42 am

    You can use DOMDocument and xpath for that.

    <?php
    $doc = new DOMDocument;
    $doc->loadhtml('<html>
      <head> 
        <title>bar , this is an example</title> 
      </head> 
      <body> 
        <h1>latest news</h1>
        foo <strong>bar</strong> 
        <i>foobar</i>
       </body>
    </html>');
    $xpath = new DOMXPath($doc);
    foreach($xpath->query('//*[contains(child::text(),"bar")]') as $e) {
      echo $e->tagName, "\n";
    }
    

    prints

    title
    strong
    i
    

    Note the i-element. It contains foobar, not bar as a single word and matches the xpath query. So this solution may or may not suffice.

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

Sidebar

Ask A Question

Stats

  • Questions 214k
  • Answers 214k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can throttle the framerate of Silverlight applications by adding… May 12, 2026 at 10:42 pm
  • Editorial Team
    Editorial Team added an answer I up-voted a couple of good answers, but came up… May 12, 2026 at 10:42 pm
  • Editorial Team
    Editorial Team added an answer Use the database to generate them. How to do that… May 12, 2026 at 10:42 pm

Related Questions

I have a swing client that renders HTML through the use of the JEditorPane
I am using the jQuery autocomplete plugin and have wired up an input field
I am writing a toolbar for IE(6+). I have used the various sample bars
I have a plain html web page with an embedded flash object. Based on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.