Solved

Match newline with preg_replace

Posted on 2006-06-15
7
1,049 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: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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

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.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

831 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