替换多个换行符,制表符和空格

I want to replace multiple newline characters with one newline character, and multiple spaces with a single space.

I tried preg_replace("/ +/", " ", $text); and failed!

I also do this job on the $text for formatting.

$text = wordwrap($text, 120, '<br/>', true);
$text = nl2br($text);

$text is a large text taken from user for BLOG, and for a better formatting I use wordwrap.

In theory, you regular expression does work, but the problem is that not all operating system and browsers send only at the end of string. Many will also send a .

Try:

I've simplified this one:

preg_replace("/(?
){2,}/", "

", $text);

And to address the problem of some sending only:

preg_replace("/[
]{2,}/", "

", $text);

Based on your update:

// Replace multiple (one ore more) line breaks with a single one.
$text = preg_replace("/[
]+/", "
", $text);

$text = wordwrap($text,120, '<br/>', true);
$text = nl2br($text);

You need the multiline modifier to match multiple lines:

preg_replace("/PATTERN/m", "REPLACE", $text);

Also in your example you seem to be replacing 2+ newlines with exactly 2, which isn't what your question indicates.

Try this:

preg_replace("/[
]*/", "
", $text); 

This is the answer, as I understand the question:

// Normalize newlines
preg_replace('/(
||
)+/', "
", $text);
// Replace whitespace characters with a single space
preg_replace('/\s+/', ' ', $text);

This is the actual function that I use to convert new lines to HTML line break and paragraph elements:

/**
 *
 * @param string $string
 * @return string
 */
function nl2html($text)
{
    return '<p>' . preg_replace(array('/(

||

)(\s+)?/', '/
||
/'),
            array('</p><p>', '<br/>'), $text) . '</p>';
}

I would suggest something like this:

preg_replace("/(\R){2,}/", "$1", $str);

This will take care of all the Unicode newline characters.

Use \R (which represents any line ending sequence):

$str = preg_replace('#\R+#', '</p><p>', $str);

It was found here: Replacing two new lines with paragraph tags

PHP documentation about Escape sequences:

\R (line break: matches , and )

I tried all of above, but it didn't work for me. Then I created some long way to resolve that issue...

Before :

echo nl2br($text);

After :

$tempData = nl2br($text);
$tempData = explode("<br />",$tempData);

foreach ($tempData as $val) {
   if(trim($val) != '')
   {
      echo $val."<br />";
   }
}

And it's worked for me.. I wrote here because, if somebody came here to find answer like me.

If you just want to replace multiple tabs with a single tab, use the following code.

preg_replace("/\s{2,}/", "\t", $string);

Replace the head and the end of string or document!

preg_replace('/(^[^a-zA-Z]+)|([^a-zA-Z]+$)/','',$match);

I have dealt with strip_tags function in PHP and had some problems like: after having a linebreak then appear a new line with some spaces and then a new linebreak appear continuously ...etc. without any rule :(.

This is my solution for dealing with strip_tags

Replace multiple spaces to one, multiple linebreaks to single linebreak

function cleanHtml($html)
{
    // Clean code into script tags
    $html = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $html);

    // Clean code into style tags
    $html = preg_replace('/<\s*style.+?<\s*\/\s*style.*?>/si', '', $html );

    // Strip HTML
    $string = trim(strip_tags($html));

    // Replace multiple spaces on each line (keep linebreaks) with single space
    $string = preg_replace("/[[:blank:]]+/", " ", $string); // (*)

    // Replace multiple spaces of all positions (deal with linebreaks) with single linebreak
    $string = preg_replace('/\s{2,}/', "
", $string); // (**)
    return $string;
}

Keywords are (*) and (**).