Solved

Match newline with preg_replace

Posted on 2006-06-15
7
1,044 Views
Last Modified: 2013-12-12
Hello,

I am using fget to grab a page from another directory on my site and display it in a window on my main page.  I am trying to trim out the footers and extra stuff I don't want displayed twice.  The tags I need to grab and replace are as follows

     </div>
<hr />

And I want to replace that with a single

</div>

I can't seem to get the regular expression quite right however, please help me express my regex

    $itemdetail = preg_replace("/<\/div>+(^[\r\n]*|[\r\n]+|[^\s]+)<hr \/>/", "</div>", $itemdetail);

I've tried everything I can think of to match the newline in between the two tags and still can't match it, please help.

Thanks in advance,
G

0
Comment
Question by:ghosting
  • 5
  • 2
7 Comments
 
LVL 40

Expert Comment

by:RQuadling
ID: 16911562
Try this ...

$itemdetail = preg_replace('`(</div>.*?<hr />)`sim", "</div>", $itemdetail);

0
 

Author Comment

by:ghosting
ID: 16911698
Nope that's not it.

I got an "unexpected ' at line xxx"

I also tried

    $itemdetail = preg_replace("/<\/div>.*?<hr />/", "</div>", $itemdetail);

and that didn't do it either, I'm still getting that horizontal rule.  Please help.
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 16911870
$itemdetail = preg_replace('`(</div>.*?<hr />)`sim', '</div>', $itemdetail);

You have to use () to actually highlight what you want to replace.

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:ghosting
ID: 16911944
Nope,
Still no go.  Tried exactly.  No syntax errors this time, but still no match.  What's the `sim' for?
I tried with and without the `sim' part just to see, either way no match.  It's just a </div> newline <hr /> that I'm trying to match and replace with a </div> tag, but I'm getting nada.  I appreciate the speedy replies as I've been banging my head on this too long and want to finish it.  Thanks for the help and looking forward to the answer.

G
0
 
LVL 40

Accepted Solution

by:
RQuadling earned 500 total points
ID: 16912080
<?php
$s_original_HTML = <<< END_HTML
<html>
<head>
<title>Dummy</title>
</head>
<body>
<div>
      <h1>This is the main header</h1>
      <hr />
</div>
<hr />
</body>
</html>
END_HTML;

$s_new_HTML = preg_replace('`(</div>.*?<hr />)`sim', '</div>', $s_original_HTML);

echo "=====\n$s_original_HTML\n=====\n$s_new_HTML\n";
?>

outputs ...

=====
<html>
<head>
<title>Dummy</title>
</head>
<body>
<div>
        <h1>This is the main header</h1>
        <hr />
</div>
<hr />
</body>
</html>
=====
<html>
<head>
<title>Dummy</title>
</head>
<body>
<div>
        <h1>This is the main header</h1>
        <hr />
</div>
</body>
</html>

0
 
LVL 40

Expert Comment

by:RQuadling
ID: 16912096
s (PCRE_DOTALL)
If this modifier is set, a dot metacharacter in the pattern matches all characters, including newlines. Without it, newlines are excluded. This modifier is equivalent to Perl's /s modifier. A negative class such as [^a] always matches a newline character, independent of the setting of this modifier.

i (PCRE_CASELESS)
If this modifier is set, letters in the pattern match both upper and lower case letters.

m (PCRE_MULTILINE)
By default, PCRE treats the subject string as consisting of a single "line" of characters (even if it actually contains several newlines). The "start of line" metacharacter (^) matches only at the start of the string, while the "end of line" metacharacter ($) matches only at the end of the string, or before a terminating newline (unless D modifier is set). This is the same as Perl.

When this modifier is set, the "start of line" and "end of line" constructs match immediately following or immediately before any newline in the subject string, respectively, as well as at the very start and end. This is equivalent to Perl's /m modifier. If there are no "\n" characters in a subject string, or no occurrences of ^ or $ in a pattern, setting this modifier has no effect.

0
 
LVL 40

Expert Comment

by:RQuadling
ID: 16918738
I guess it worked then?

Where you not catching the output of preg_replace?
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

746 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

15 Experts available now in Live!

Get 1:1 Help Now