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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:26:08+00:00 2026-06-10T02:26:08+00:00

Having a problem with the following preg_replace: $subject = ‘<div class=main> <div class=block_bc> <a

  • 0

Having a problem with the following preg_replace:

$subject = '<div class="main"> <div class="block_bc"> <a href="index.php?x_param=11" class="BC-1"> Gallery</a> / <a href="path/Title_Item/?x_param=17" class="BC-2"> Title Item</a> / <span class="BC-3"> Bridge</span> </div> </div>';
$regex = '/(<div\sclass=\"block_bc\"[^>]*>)([^<\/div>]*>)(<\/div>)/is';   
$replacement = '<div class="block_bc"></div>';
preg_replace($regex, $replacement, $subject);

Basically, I want to end up with <div class="main"> <div class="block_bc"></div> </div> but it is not getting selected.

Can anyone please point me to the “obvious” error?

  • 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-10T02:26:10+00:00Added an answer on June 10, 2026 at 2:26 am

    You try to use character classes ([]) wrong. The [^<\/div>]* part means that number of characters except one of the following: <,/,d,i,v,>. This probably not what you meant.

    What you could use is non-greedy repeat:

    $regex = '/(<div\s*class=\"block_bc\"[^>]*>)(.+?)(<\/div>)/is';
    

    Also, getting things out from html with regexp can be extremely brittle, try using the DOM for this with xpath. It’s more verbose but also more resilient for badly formatted input:

    $subject = '<div class="main"> <div class="block_bc"> <a href="index.php?x_param=11" class="BC-1"> Gallery</a> / <a href="path/Title_Item/?x_param=17" class="BC-2"> Title Item</a> / <span class="BC-3"> Bridge</span> </div> </div>';
    
    libxml_use_internal_errors(true); // supress warnings
    $doc = new DOMDocument;
    $doc->loadHTML($subject);
    
    $xpath = new DOMXpath($doc);
    // get the <div class="main"> node for exporting
    $main_node  = $xpath->query('//div[@class="main"]');
    // select the block_bc classed div's childs, and the textnodes under it
    $childNodes = $xpath->query('//div[@class="block_bc"]/* | //div[@class="block_bc"]/text()'); 
    foreach ($childNodes as $c) {
        $c->parentNode->removeChild($c); // clear them all
    }
    
    // export the part of the document under the <div class="main">
    print $doc->saveHTML($main_node->item(0)); 
    // update:
    // if you want the full document in html you can simply omit the parameter, with this you can get rid of the $main_node = ... line too
    print $doc->saveHTML(); // this will print from doctype to </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I´m having the following problem: I have 2 classes (A and B). The class
I'm currently having a small problem with the following regex in PHP: preg_match_all('/(.*)(XS|S|M|XL|XXL|L)(.*)/i', '00236XL00',
I am having the following problem: public class A { public A(X, Y, Z)
I'm having following problem. I should convert a control to a certain type, this
I am having the following problem: a) I have a UNIX build environment set
I'm having the following problem and wondered whether anyone could see why this is
I'm having the following problem using the Visual Studio 2010 Team System Beta 1:
I'm having the following problem: When I got two labels into each other: <Label
I'm having the following problem when using GWT and request factory. I am working
I'm having a problem with the following code foreach ($ex in (foo, bar, baz))

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.