Jump to content

converting plain text to HTML errors


boom

Recommended Posts

Hi i found a script to convert Plain Text to HTML, it works apart from when ever if formats a link it doesn't do it properly:

 

<?php

// set source file name and path
$source = $_POST['message'];

// read raw text as array
$data = $source or die("Cannot read file");

// replace special characters with HTML entities
// replace line breaks with <br />
$html = nl2br(htmlspecialchars($data));

// replace multiple spaces with single spaces
//$html = preg_replace('/\s\s+/', ' ', $html);

// replace URLs with <a href...> elements
$html = preg_replace('/\s(\w+:\/\/)(\S+)/', ' <a href="\\1\\2" target="_blank">\\1\\2</a>', $html);

// start building output page
// add page header
$output =<<< HEADER
<html>
<head>
</head>
<body>
HEADER;

// add page content
$output .= "<div>$html</div>";

// add page footer
$output .=<<< FOOTER
</body>
</html>
FOOTER;

// display in browser
echo $output;
?>

 

say i inputted the following into a text box

 

test 123

 

blar blar blar blar

blar blar blar blar

 

http://www.blar.com

 

the output would be:

 

test 123

 

blar blar blar blar

blar blar blar blar

 

http://www.blar.com<br/

/>

 

and it's that <br/

/> that i want to get sorted the only way i've found is to enclude text on the same line as the link after it.

 

Please help.

 

Cheers

 

Paul

Link to comment
https://forums.phpfreaks.com/topic/79169-converting-plain-text-to-html-errors/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.