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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.