Solved

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

Posted on 2008-10-20
8
768 Views
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">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>
 

<body>

<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>

<table>

  <tr>

    <td>This</td>

    <td>table</td>

  </tr>

  <tr>

    <td>is</td>

    <td>dead</td>

  </tr>

</table>
 

</div>

</body>

</html>

Open in new window

0
Comment
Question by:dban00b
8 Comments
 
LVL 19

Expert Comment

by:BrianGEFF719
Comment Utility
Hi,
Try this:

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

0
 
LVL 19

Expert Comment

by:BrianGEFF719
Comment Utility
Also, that regex should be in single quotes, not double.
0
 
LVL 27

Expert Comment

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

i = case-insensitive
s = . matches any character, including \n
0
 
LVL 23

Expert Comment

by:Tiggerito
Comment Utility
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?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 27

Accepted Solution

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

Expert Comment

by:ddrudik
Comment Utility
Thanks for the question and the points.
0
 
LVL 1

Author Comment

by:dban00b
Comment Utility
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
0
 
LVL 27

Expert Comment

by:ddrudik
Comment Utility
here's a description of the different modifiers, s is required for your source since it contains \n:
http://us2.php.net/manual/en/reference.pcre.pattern.modifiers.php
here's a description of delimiters, I used # since / was in the pattern, although it works to escape / with \ within the pattern:
http://us2.php.net/manual/en/intro.pcre.php
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
htacces issue 2 37
Export MYSQL to .CSV Add Column Names PHP 2 21
wordpress issue 2 15
Filter wordpress query 3 5
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now