Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Match newline with preg_replace

Posted on 2006-06-15
7
Medium Priority
?
1,071 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.
Suggested Courses

597 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