开发者

nl2br for paragraphs

开发者 https://www.devze.com 2023-01-16 09:56 出处:网络
$variable = \'Afrikaans Shqip - Albanian Euskara - Basque\'; How do I convert each new line to paragraph?
$variable = 'Afrikaans 
Shqip - Albanian  
Euskara - Basque';

How do I convert each new line to paragraph?

$variable should become:

<p>Afrikaans</p>
<p>Shqip - Albania开发者_运维知识库n</p>
<p>Euskara - Basque</p>


Try this:

$variable = str_replace("\n", "</p>\n<p>", '<p>'.$variable.'</p>');


The following should do the trick :

$variable = '<p>' . str_replace("\n", "</p><p>", $variable) . '</p>';


Be careful, with the other proposals, some line breaks are not catch. This function works on Windows, Linux or MacOS :

function nl2p($txt){
    return str_replace(["\r\n", "\n\r", "\n", "\r"], '</p><p>', '<p>' . $txt . '</p>');
}


$array = explode("\n", $variable);
$newVariable = '<p>'.implode('</p><p>', $array).'</p>'


<?php
$variable = 'Afrikaans  
Shqip - Albanian   
Euskara - Basque'; 
$prep0 = str_replace(array("\r\n" , "\n\r") , "\n" , $variable);
$prep1 = str_replace("\r" , "\n" , $prep0);
$prep2 = preg_replace(array('/\n\s+/' , '/\s+\n/') , "\n" , trim($prep1));
$result = '<p>'.str_replace("\n", "</p>\n<p>", $prep2).'</p>'; 
echo $result;
/*
<p>Afrikaans</p>
<p>Shqip - Albanian</p>
<p>Euskara - Basque</p>
*/
?>

Explanation:

$prep0 and $prep1: Make sure each line ends with \n.

$prep2: Remove redundant whitespace. Keep linebreaks.

$result: Add p tags.

If you don't include $prep0, $prep1 and $prep2, $result will look like this:

<p>Afrikaans  
</p>
<p>Shqip - Albanian   
</p>
<p>Euskara - Basque</p>

Not very nice, I think.

Also, don't use preg_replace unless you have to. In most cases, str_replace is faster (at least according to my experience). See the comments below for more information.


Try:

$variable = 'Afrikaans
Shqip - Albanian  
Euskara - Basque';

$result = preg_replace("/\r\n/", "<p>$1</p>", $variable);
echo $result;


I know this is a very old thread, but I want to highlight, that suggested solutions can have some issues in HTML world:

  1. They do not check whether there is already a p tag around respective paragraph. This can result in extra paragraphs. At least some browsers will then show this as extra paragraphs, meaning <p>line1<p>line2</p>line3</p> will result in 3 paragraphs, which may not be the intention.
  2. In fact, there is a bunch of tags, that are not expected inside of p, as per the spec of phrasing content. Or rather there is a limited set of tags, tha can be.
  3. They will change new lines inside tags, where you want to preserve new lines as is. pre and textarea are the ones, where you could generally want that. code, samp, kbd and var are an example of other common values, but technically it can be any tag with white-space CSS property set to either pre, pre-wrap, pre-line or break-spaces.
  4. They usually only check for \r\n or just \r or \n, while there are actually more symbols, that would mean new line, and they also have respective HTML entities, which can easily occur in HTML string.

To "combat" these flaws, at least, to an extent, I've just released a nl2tag library, which can also "convert" new lines to <li> items and has an "improved" nl2br logic (mostly for the sake of whitespace retention).
It's far from perfect (check the readme for limitations), but should cover you in case of relatively simple HTML string.

0

精彩评论

暂无评论...
验证码 换一张
取 消