How to replace new lines by regular expressions

此生再无相见时 提交于 2020-01-25 08:35:32

问题


Can you help me with replacing in PHP. I don't 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/>, between two paragraphs.

Thanks a lot.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/3536796/how-to-replace-new-lines-by-regular-expressions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!