?
Solved

Match newline with preg_replace

Posted on 2006-06-15
7
Medium Priority
?
1,065 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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…
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 …
Suggested Courses

801 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