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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:41:56+00:00 2026-06-09T03:41:56+00:00

Possible Duplicate: How to get text between div tags with id being 1st attribute.

  • 0

Possible Duplicate:
How to get text between div tags with id being 1st attribute. Only with regular expressions.

Here is the scenario

I’m using this regular expression for getting the text inside div tag with id test1,or tes2 or test3

<div id = "test1">text</div>
<div id = "test2">text</div>
<div id = "test3">text</div>

$id_value = "test1" or "test2" or "test3";
$regex = "#\<div id=\"".$id_value."\"\>(.+?)\<\/div\>#s";

My only requirement is to get the text from div tags in below scenario

<div id="test" class="testing" style="color:red" etc etc .... more attributes >text</div>

i.e id being the 1st attribute of div tag and can be followed by n number of attributes. How to extract text from such a tag by regex only.

I even tried

$regex = '#<div\s+.*?id="".$id_value."".*?>(.*?)</\s*div>#ims';

it is returning text of div tag when $id_value = “test1″ but if $id_value=”test2″ it is returning text values of both nodes test1 and test2. If $id_value=”test3” it is returning text value of all 3 nodes. I need text value related to specific id only. Using Regex only.

Please help Thanks You.

  • 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-09T03:41:57+00:00Added an answer on June 9, 2026 at 3:41 am

    Don’t use RegExp to parse HTML. Instead, use PHP’s DOM extension which can correctly parse any kind of HTML.

    Example:

    <?php
    
        $html = <<<HTML
    <div id = "test1">text</div>
    <div id = "test2">other text</div>
    <div id = "test3">new text</div>
    HTML;
    
        $id_list = array(
            "test1",
            "test2",
            "test3",
        );
    
        $doc = new DOMDocument();
        $doc->loadHTML($html);
        foreach ($id_list as $id) {
            $div = $doc->getElementById($id);
            if ($div == NULL) {
                echo "There's no element with an ID of $id<br>\n";
            }
            else {
                echo "$id's content is: " . $div->textContent . "<br>\n";
            }
        }
    

    If and only if you absolutely must use RegExp, this is what I came up with:

    <?php
    
        $html = <<<HTML
    <div id = "test1">text</div>
    <div id = "test2">other text</div>
    <div id = "test3">new text</div>
    HTML;
    
        $id_list = array(
            "test1",
            "test2",
            "test3",
        );
    
        foreach ($id_list as $id) {
            $pattern = <<<REGEX
    /
    <div\s*                     #Opening Tag
    (?:                         #Attributes before ID
        [a-z]+                  #Attribute name
        \s*=\s*                 #Equals
        (?:"[^"]*"|'[^']*')     #Attribute content
        \s*                     #Spaces?
    )*                          #Many or none
    (?:                         #ID Attribute
        id
        \s*=\s*
        (?:"$id"|'$id')         #Matches the ID
        \s*
    )
    [^>]*                       #Anything after ID
    >                           #Closing Tag
    ([^<]*)                     #Actual content!
    <\/div>
    /xi
    REGEX;
    
            preg_match_all($pattern, $html, $matches);
            var_dump($matches);
        }
    

    Beware, if you use this code, the unh̶oly͘ ͘c̀h̶i͏l҉d wį
    ll weep the blood of virgins. The <center> cannot hold it is too late.

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

Sidebar

Related Questions

Possible Duplicate: PHP Regex Get Text Between BBCode Tags I have a text like:
Possible Duplicate: Get content between two strings PHP Ok, here is my delima: here
Possible Duplicate: Get a list of dates between two dates This is my sql:
Possible Duplicate: How do I get this CSS text-decoration issue to work? I'm using
Possible Duplicate: Why does PHP echo'd text lose it's formatting? I cant get the
Possible Duplicate: How do I get this CSS text-decoration override to work? Take a
Possible Duplicate: Iphone UITextField only integer I want to place a text field that
Possible Duplicate: css vertical centering I'm trying to get text centered in a vertical
Possible duplicate: jquery change div text The above question is a possible duplicate, but
Possible Duplicate: jQuery get selected text from dropdownlist <select id=id_deals name=deals multiple=multiple> <option value=1>deal

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.