note 27484 modified in function.nl2br by aidan
| From: | aidan@php.net | Date: | Sun, 20 Jun 2004 09:11:33 +0000 |
| Subject: | note 27484 modified in function.nl2br by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-71398@lists.php.net to get a copy of this message | ||
Hanging indents:
Regarding Matt's note of 15-Oct-2002: I like the idea of having most of my html in a plain text
file, and then PHP automatically wrapping it with paragraph tags. I am also fond of hanging
indentations which I indicate by a normal line, followed by tabs in subsequent lines. This whole
section I wrap in a <hang>...</hang> tag (so sections separated by two newlines will
receive a break). Here's how I parse it (there is some variation to the autoP function):
<?php
function hangPar ($text) {
$hangP = "<P style=text-indent:-2em;margin-left" .
":2em;margin-top:0;margin-bottom:0>";
$pre = "^(?!<P\\b)(?!\\t)(.+)";
while (preg_match("/^(.*)<hang>(.*?)<\\/hang>(.*)$/s", $text, $matches)) {
// turn lines followed by tabbed lines into hanging indents
$hang = preg_replace("/$pre(((\\n\\t).*)+)/mi", "$hangP$1<br >$2</P
>", $matches[2]);
// turn singleton lines without tabs into solo paragraphs
$hang = preg_replace ("/$pre\\n(?!\\t)/mi", "$hangP$1</P >\n",
$hang);
// break between sections
$hang = preg_replace ("/\\n\\n+/", "\n<br >", $hang);
$text = $matches[1] . $hang . $matches[3];
}
return $text;
}
function autoP($txt) {
$txt = preg_replace("/(\r\n|\n|\r)/", "\n", $txt); // cross-platform
newlines
$txt = preg_replace("/\n\n+/", "\n\n", $txt); // take care of duplicates
$txt = hangPar($txt); // do hanging indents
// bold single lines - assume they're headings
$txt = preg_replace('/\n\n([^\n]+)\n\n/', "\n<br ><br ><font
size=+1><b>$1</b></font>\n\n", $txt);
// make paragraphs
$txt = preg_replace("/\\n\\n(?!<P\\b)(.+?)(\\n\\n|\\z)/msi", "\n\n<p
>$1</p >\n\n", $txt);
$txt = preg_replace("/\\n\\n(?!<P\\b)(.+?)(\\n\\n|\\z)/msi", "\n\n<p
>$1</p >\n\n", $txt);
return $txt;
}
?>
NOTE: The look of negative lookahead - (?! - in line 9,10 of autoP could be extended to preclude
certain or all elements (such as tables by means of (?!<table\\b)) from being wrapped in
paragraph tags. As Matt noted, there has been a space added after the br, P, and /P tags for
formatting reasons. You should remove those (from lines 6,8,10 of hangPar and lines 7,9, 10 of
AutoP). Csaba Gabor
--was--
Hanging indents:
Regarding Matt's note of 15-Oct-2002: I like the idea of having most of my html in a plain text
file, and then PHP automatically wrapping it with paragraph tags. I am also fond of hanging
indentations which I indicate by a normal line, followed by tabs in subsequent lines. This whole
section I wrap in a <hang>...</hang> tag (so sections separated by two newlines will
receive a break). Here's how I parse it (there is some variation to the autoP function):
function hangPar ($text) {
$hangP = "<P style=text-indent:-2em;margin-left" .
":2em;margin-top:0;margin-bottom:0>";
$pre = "^(?!<P\\b)(?!\\t)(.+)";
while (preg_match("/^(.*)<hang>(.*?)<\\/hang>(.*)$/s", $text, $matches)) {
// turn lines followed by tabbed lines into hanging indents
$hang = preg_replace("/$pre(((\\n\\t).*)+)/mi", "$hangP$1<br >$2</P
>", $matches[2]);
// turn singleton lines without tabs into solo paragraphs
$hang = preg_replace ("/$pre\\n(?!\\t)/mi", "$hangP$1</P >\n",
$hang);
// break between sections
$hang = preg_replace ("/\\n\\n+/", "\n<br >", $hang);
$text = $matches[1] . $hang . $matches[3];
}
return $text;
}
function autoP($txt) {
$txt = preg_replace("/(\r\n|\n|\r)/", "\n", $txt); // cross-platform
newlines
$txt = preg_replace("/\n\n+/", "\n\n", $txt); // take care of duplicates
$txt = hangPar($txt); // do hanging indents
// bold single lines - assume they're headings
$txt = preg_replace('/\n\n([^\n]+)\n\n/', "\n<br ><br ><font
size=+1><b>$1</b></font>\n\n", $txt);
// make paragraphs
$txt = preg_replace("/\\n\\n(?!<P\\b)(.+?)(\\n\\n|\\z)/msi", "\n\n<p
>$1</p >\n\n", $txt);
$txt = preg_replace("/\\n\\n(?!<P\\b)(.+?)(\\n\\n|\\z)/msi", "\n\n<p
>$1</p >\n\n", $txt);
return $txt;
}
NOTE: The look of negative lookahead - (?! - in line 9,10 of autoP could be extended to preclude
certain or all elements (such as tables by means of (?!<table\\b)) from being wrapped in
paragraph tags. As Matt noted, there has been a space added after the br, P, and /P tags for
formatting reasons. You should remove those (from lines 6,8,10 of hangPar and lines 7,9, 10 of
AutoP). Csaba Gabor
http://php.net/manual/en/function.nl2br.php