On Thursday 15 February 2001 04:31, Dallas Kropka wrote:
> I have a few text input boxes that I am using to allow the client to
> compose emails for a mailing list... does anyone have a little snippet
> that will take the "\n's" and turn them into <BR>'s? I would attempt to
> write one myself.... but Im not the best at ereg or replace code...
nl2br () is the simple solution (as others already mentioned).
Attached is a more sophisticated one I use in my code. It detects
paragraphs (empty lines), and highlights URLs and email adresses.
--
Christian Reiniger
LGDC Webmaster (http://sunsite.dk/lgdc/)
I saw God --------- and she was black.
<?php
/*
* $Id: text2html.php,v 1.7 2001/01/07 21:03:23 chris Exp $
*/
function pbText2HTML ($Text, $LinkStyleClass = '', $LinkTarget = '')
{
$NewText = "";
$LastWasBlank = true;
// (1) quote all HTML entities (assumes iso8859-1)
$Text = htmlentities ($Text);
// (2) Divide into paragraphs and
// (3) Add markup for URLs
foreach (explode ("\n", $Text) as $CLine)
{
$CLine = trim ($CLine);
if ($CLine == '') {
if ($LastWasBlank) {
// ignore multiple blank lines
continue;
}
else {
$LastWasBlank = true;
$NewText .= "</p>\n";
}
}
else {
if ($LastWasBlank) {
$LastWasBlank = false;
$NewText .= "<p>\n";
}
$NewText .= pb_t2h_URLMarkup ($CLine,
$LinkStyleClass,
$LinkTarget);
$NewText .= "\n";
}
}
return $NewText;
}
function pb_t2h_URLMarkup ($Text, $StyleClass = '', $Target = '')
{
if ($StyleClass != '')
$ClassS = " class='$StyleClass'";
else
$ClassS = "";
if ($Target != '')
$TargetS = " target='$Target'";
else
$TargetS = '';
$Patterns = array ('/([^"\'])(http:\/\/[\w\.\/~\-]+)/',
'/([^"\'])(ftp:\/\/[\w\.\/~\-]+)/',
'/([^"\'\w-\.\/])(www\.[\w\.\/~\-]+)/',
'/^(www\.[\w\.\/~\-]+)/',
'/([\w\.~\-=]+@[\w\.~\-]+)/');
$Replacements = array ("\\1<a href='\\2'$ClassS$TargetS>\\2</a>",
"\\1<a href='\\2'$ClassS$TargetS>\\2</a>",
"\\1<a href='http://\\2'$ClassS$TargetS>\\2</a>",
"<a href='http://\\1'$ClassS$TargetS>\\1</a>",
"<a href='mailto:\\1'$ClassS>\\1</a>");
return preg_replace ($Patterns, $Replacements, $Text);
}
?>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]