mesnicka
mesnicka

Reputation: 2548

How to replace new lines by regular expressions

How can I set any quantity of new lines with a regular expression?

$var = "<p>some text</p><p>another text</p><p>more text</p>";
$search = array("</p>\s<p>");
$replace = array("</p><p>");
$var = str_replace($search, $replace, $var);

I need to remove every new line (\n), not <br/>, between two paragraphs.

Upvotes: 0

Views: 2445

Answers (2)

Yanick Rochon
Yanick Rochon

Reputation: 53516

I find it odd to have huge HTML strings, and then using some string search and replace hack to format that afterwards...

When constructing HTML with PHP, I like using arrays:

$htmlArr = array();
foreach ($dataSet as $index => $data) {
   $htmlArr[] = '<p>Line#'.$index.' : <span>' . $data . '</span></p>';
}

$html = implode("\n", $htmlArr);

This way, every HTML line has its separate $htmlArr[] value. Moreover, if you need your HTML to be "pretty print", you can simply have some sort of method that will indent your HTML by prepending whitespaces at the beginning of every array elements depending on some rule set. For example, if we have:

$htmlArr = array(
  '<ol>',
  '<li>Item 1</li>',
  '<li><a href="#">Item 2</a></li>',
  '<li>Item 3</li>',
  '</ol>'
);

Then the formatting function algorithm would be (a very simple one, considering that the HTML is well constructed):

$indent = 0; // Initial indent
foreach & $value in $array
   $open = Count how many opened elements
   $closed = Count how many closed elements
   $value = str_repeat(' ', $indent * TAB_SPACE) . $value;
   $indent += $open - $closed;  // Next line's indent
end foreach

return $array

Then implode("\n", $array) for the prettyfied HTML

After the question edit by Felix Kling, I realize that this has nothing to do with the question. Sorry about that :) Thanks though for the clarification.

Upvotes: 0

Peter Ajtai
Peter Ajtai

Reputation: 57685

To begin with, str_replace() (which you referenced in your original question) is used to find a literal string and replace it. preg_replace() is used to find something that matches a regular expression and replace it.

In the following code sample I use \s+ to find one or more occurrences of white space (new line, tab, space...). \s is whitespace, and the + modifier means one or more of the previous thing.

<?php
  // Test string with white space and line breaks between paragraphs
$var = "<p>some text</p>    <p>another text</p>
<p>more text</p>";

  // Regex - Use ! as end holders, so that you don't have to escape the
  // forward slash in '</p>'. This regex looks for an end P then one or more (+)
  // whitespaces, then a begin P. i refers to case insensitive search.
$search = '!</p>\s+<p>!i';

  // We replace the matched regex with an end P followed by a begin P w no
  // whitespace in between.
$replace = '</p><p>';

  // echo to test or use '=' to store the results in a variable. 
  // preg_replace returns a string in this case.
echo preg_replace($search, $replace, $var);
?>

Live Example

Upvotes: 4

Related Questions