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

Related Questions

I have a HTML document with the a table which I want to replace,
I have XML tag that has the content which is HTML document. <xml-tag> <!--
I have a html document and I want to delete all the divs of
I have an html document with multiple commented-out PHP arrays, e.g.: <!-- Array (
I have a large malformed test HTML document which I need to get the
Hey can any one suggest for web view. I have html string which has
I have a string that represents an html document. I'm trying to replace text
For instance I have a string: $string = '<div class=ImageRight style=width:150px>'; which I want
I have a HTML document that I want to remove specific tags from, identified
I have an html document that I saved as a .txt file. I want

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.