Solved

Match newline with preg_replace

Posted on 2006-06-15
7
1,054 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 40

Expert Comment

by:Richard Quadling
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:Richard Quadling
ID: 16911870
$itemdetail = preg_replace('`(</div>.*?<hr />)`sim', '</div>', $itemdetail);

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

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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:
Richard Quadling 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:Richard Quadling
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:Richard Quadling
ID: 16918738
I guess it worked then?

Where you not catching the output of preg_replace?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

733 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