tags:

views:

68

answers:

2

Hello,

can you help me with replacing in PHP. I dont know how to set any quantity of new lines with regex.

$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);

What i need, is to remove every new line (\n), not <br/>, beetween two paragraphs.

Thanks a lot.

+2  A: 

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

Peter Ajtai
+1 `str_replce()` should be sufficient in this case.
Felix Kling
It works. Thanks a lot.
A: 

I find it odd to have huge HTML strings, 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 it's 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 somme 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 (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

** UPDATE **

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.

Yanick Rochon