php regex find everything (any character any white space any lline breaks) between div tags

Posted on 2008-10-20
Medium Priority
Last Modified: 2008-10-21
I'm pretty new to using regular expressions and through trial and error, I've been able to figure out pretty much everything except this:

How can I find everything? Every character, every whitespace, every line break?

For example I want to capture everything between to html tags: <div class="thisdiv"></div>

$pattern="/     ?    /";
$replacement="<div class=\"thatdiv\"><strong>thisdiv</strong> has been replace</div>";
include(preg_replace($pattern,$replacement,"testfile.html" ));

Thanks for any help figuring out my pattern.  The line breaks are \r\n
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<div class="thisdiv">All the contents of this div need to be replaced <br />
<p>this paragraph---goners</p>
<div>this div dies too</div>

Open in new window

Question by:dban00b
LVL 19

Expert Comment

ID: 22764722
Try this:

$pattern="/<div class=\"thisdiv\">(.*?)<\/div>/ix";

LVL 19

Expert Comment

ID: 22764961
Also, that regex should be in single quotes, not double.
LVL 27

Expert Comment

ID: 22765777
Assuming no nested div's:
$pattern='#<div class="thisdiv">(.*?)</div>#is';

i = case-insensitive
s = . matches any character, including \n
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

LVL 23

Expert Comment

by:Tony McCreath
ID: 22765814
Looks like the example has nested div's!

Unless you can add more rules you might find this a hard one to solve.

Would this div always be the outermost div of the file?
LVL 27

Accepted Solution

ddrudik earned 2000 total points
ID: 22765874
Assuming outer-most DIV with/without nested DIVs:
$pattern='#<div class="thisdiv">(.*)</div>#is';
LVL 27

Expert Comment

ID: 22767449
Thanks for the question and the points.

Author Comment

ID: 22767510
The accepted one worked right away, just blindly copying the pattern into my source. I would like to know something extra through::

$pattern='#<div class="thisdiv">(.*?)</div>#is'; // stops at the nested </div>
$pattern='#<div class="thisdiv">(.*)</div>#is'; //stops at the last </div> in the file

I assume the hash marks are to avoid having to escape the "/" , but how does removing the "?"make the match skip the nested the div's?  And what does the 'x' tag do? Because the first answer:
$pattern='/<div class=\"thisdiv\">(.*?)<\/div>/ix'; //looks very similar, but returned an empty array
LVL 27

Expert Comment

ID: 22767543
here's a description of the different modifiers, s is required for your source since it contains \n:
here's a description of delimiters, I used # since / was in the pattern, although it works to escape / with \ within the pattern:

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This article discusses how to implement server side field validation and display customized error messages to the client.
I recently worked on a Wordpress site that utilized the popular ContactForm7 (https://contactform7.com/) plug-in that only sends an email and does not save data. The client wanted the data saved to a custom CRM database. This is my solution.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question