By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,830 Members | 2,271 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,830 IT Pros & Developers. It's quick & easy.

regular expression help

P: n/a
I'm trying to basically remove chunks of html from a page but I must
not be doing my regular expression correctly.

What i'm trying with no avail.
$site = preg_replace("/<!DOCTYPE(.|\s)*<div class=\"notice_tan\">(.|
\s)*</div>/", "", $site);

I'm trying to remove from the very top to a specific div

Top of file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

[Multiple lines of html to]

<div class="notice_tan">
[more html]
</div>

I want to remove those lines with everything in between.
I hope this all makes sense
Sep 21 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Rene wrote:
$site = preg_replace("/<!DOCTYPE(.|\s)*<div class=\"notice_tan\">(.|
\s)*</div>/", "", $site);
Here you have a slash as part of </div>, which preg_replace() will think
is the end of the pattern. You don't have to use slashes to mark the
regex, you can use any character you want, like so:
preg_replace("^regex^", ..., ...)
This makes escaping slashes unnecessary.

You probably use (.|\s) because you don't know about the s pattern
modifier. If this modifier is set, a dot metacharacter in the pattern
matches all characters, including newlines.
Sep 21 '08 #2

P: n/a
ok so remove the beginning and ending slashes and how do I hangle the
</divso it doesn't think it's the end of the expression?

Sep 21 '08 #3

P: n/a
In our last episode,
<e1**********************************@t54g2000hsg. googlegroups.com>, the
lovely and talented Rene broadcast on comp.lang.php:
ok so remove the beginning and ending slashes and how do I hangle the
</divso it doesn't think it's the end of the expression?
Escape it with a backslash.

However, regular expressions simply are not up the task of parsing HTML.
They are not the right tool for the job.

--
Lars Eighner <http://larseighner.com/us****@larseighner.com
"We have no opinion on your Arab - Arab conflicts, such as your dispute with
Kuwait." -- Bush's Ambassador April Glaspie, giving Saddam Hussein
the greenlight to invade Kuwait.
Sep 21 '08 #4

P: n/a
Lars Eighner wrote:
However, regular expressions simply are not up the task of parsing HTML.
They are not the right tool for the job.
What are?
Sep 21 '08 #5

P: n/a
..oO(Sjoerd)
>Lars Eighner wrote:
>However, regular expressions simply are not up the task of parsing HTML.
They are not the right tool for the job.

What are?
DOM

Micha
Sep 21 '08 #6

P: n/a
In our last episode, <de***************************@news.chello.nl>, the
lovely and talented Sjoerd broadcast on comp.lang.php:
Lars Eighner wrote:
>However, regular expressions simply are not up the task of parsing HTML.
They are not the right tool for the job.
What are?
Nothing in PHP that I know of. Perl has html parsing modules. You probably
could roll your own with one of the off-the-shelf parsers --- but this would
almost certainly be more work than manually editing some number of page one
time.

To see what is wrong with regular expressions in cases like this, back up to
the original post. The OP wants to match up to a certain </div>. But
regular expressions are greedy, so he will match the last </divin the
document. If you make the regular expressions less greedy, you may match a
</divthat is nested in the div you want to match.

Sure, I use perl one liners to alter html documents sometimes, and use
regular expressions in my editor to make html changes in single documents.
If you know and control the source documents, so you know they are valid to
begin with and you know what is in them and sometimes even how they are
formated (such has how many tabs to the </divyou want), you can use
regular expressions to cut corners sometimes, but there is no regular
expression answer to the question as put by the OP.
--
Lars Eighner <http://larseighner.com/us****@larseighner.com
College: The fountains of knowledge, where everyone goes to drink.
Sep 21 '08 #7

P: n/a
Michael Fesser <ne*****@gmx.dewrites:
.oO(Sjoerd)
>>Lars Eighner wrote:
>>However, regular expressions simply are not up the task of parsing HTML.
They are not the right tool for the job.

What are?

DOM
DOM isn't a tool for parsing HTML. A DOM tree can, however, be the
output of such a tool.

sherm--

--
My blog: http://shermspace.blogspot.com
Cocoa programming in Perl: http://camelbones.sourceforge.net
Sep 21 '08 #8

P: n/a
Sjoerd <sj******@gmail.comwrites:
Lars Eighner wrote:
>However, regular expressions simply are not up the task of parsing HTML.
They are not the right tool for the job.

What are?
HTML parsers.

sherm--

--
My blog: http://shermspace.blogspot.com
Cocoa programming in Perl: http://camelbones.sourceforge.net
Sep 21 '08 #9

P: n/a
..oO(Sherm Pendley)
>Michael Fesser <ne*****@gmx.dewrites:
>.oO(Sjoerd)
>>>Lars Eighner wrote:
However, regular expressions simply are not up the task of parsing HTML.
They are not the right tool for the job.

What are?

DOM

DOM isn't a tool for parsing HTML. A DOM tree can, however, be the
output of such a tool.
OK, accepted. But actually the OP doesn't really want to parse the HTML,
but modify parts of it. Performing this task should be quite easy with
an XPath query, applied to the DOM of his document.

Micha
Sep 21 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.