Forum Moderators: coopster

Message Too Old, No Replies

MySQL storage OR calling - turning a line break to <br />

         

Readie

11:05 pm on Jan 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Right, I'm wondering if it's possible to do either of the following:

1. Have a form with the MySQL INSERT command, that contains a text area. If the user drops down 2 lines and hits submit, it will save their input in the database as "blah blah<br /><br />blah blah".

2. If a a line is dropped down inside a database, when that entry is called, line breaks are turned into <br />.

I've looked up [php.net...]

But because I'm making a page for other people to enter data into the database I need to make it as simple as possible, as these users will not be able to modify their post (until I get around to coding a page to modify previous posts) if they absent mindedly just tap enter a few times rather than typing \n

Any help would be very gratefully recieved :)

Regards,
Readie

rocknbil

1:52 am on Jan 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, break tags are not what you want. Sure, they give you the visual appearance, but it's not semantic.

What I do is detect if there is any allowed markup. That is, if you set down a system of allowed tags, using [] or even live carats <>. and detect these in input, it's likely the user is doing their own markup.

If there's no tags in the input, look for double lines. If found, sub the inner ones with </p><p>, chop excess leading and trailing space, then wrap the whole thing in <p> and <p>.

You'd do this with regexps, it's not that difficult, just be sure to sub MORE than 2 lines with a single </p><p> set.

Wife's going to kill me for even being on here, we're going out, so if you can't sort it, maybe some code tomorrow. :-P

Readie

5:19 pm on Jan 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, I certainly appreciate you risking your hide to answer one of my posts then :) :P

In respect to the system of allowed tags, I've already developed a system for that using 2 ksorted arrays (< > " ' are turned into numerical codes to prevent HTML being used, [ b][ /b](unspaced) etc is detected and replaced with HTML) and a preg_replace string - so that's sorted

I guess to clarify what my problem is: I need to know how to detect a line break so that I can replace it, the actual replacement is an item I've already learnt.

You say it's done with regex, so I think it'll go something like:

$original = array();
$original[0] = preg_match(regex-for-2-or-more-line-breaks);
$original[1] = preg_match(regex-for-1-line-break);

$replacement = array();
$replacement[0] = '</p><p>';
$replacement[1] = '<br />';

ksort($original);
ksort($replacement);

and then:

preg_replace($original, $replacement, $textarea);

... Am I close? :)

As for what the regex is, my understanding is still somewhat hampered there as I've been focusing my learning efforts elsewhere :)

Regards,
Readie

Readie

10:18 pm on Jan 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Bit of sand in my face here, but I don't have the method for using regex as the identifier in a preg_replace function set in stone. After perusing a very useful website with a table of regex... stuff :) I managed to write the following:

[/n]{2,}$
[/n]{1}$

to identify 2+ lines, or just the 1 line.

Getting this in my array, however, is presenting me with difficulties, as I cannot find any reference for using preg_replace with a regex defined in an array.

And acctually looking up the syntax of preg_match kind of scrapped my above idea :)

Regards,
Readie

rocknbil

3:18 am on Jan 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've done this so many times I can't remember what worked best, or where the heck the code is . . .

Here's one I found, originally in Perl, converted to PHP, it's not a one-liner and not the greatest of all but works.

No filtering or limitation on the types of tags, you're probably going to want to create a list of acceptable tags and filter the input accordingly if markup is found. Better, use BB style brackets [] or htmlentity before actually doing anything with it. Tested and working, for the most part.

<?php
header("content-type:text/html");

$formatted = '<p>This is

some formatted

content

</p>

';

$br = '
horrible <br>

or worse <br><br />

break tags.

';

$unformatted = 'This is some unformatted content.

Pretty much what you\'d expect.

Well, except for the escapes.

Experiment with multiple line spaces for

forum space abusers.

';

echo "$formatted $br $unformatted";
echo "<hr><p>&nbsp;</p>\n\n";

$formatted = webify_output($formatted);
$br = webify_output($br);
$unformatted = webify_output($unformatted);

echo "$formatted $br $unformatted";


function webify_output($content_block) {
//If the content contains no <p> or <br>, it's a safe bet it needs to be formatted
if (! preg_match('/\<p¦br[^>]*\s*\/*>/im',$content_block)) {
// so they don't double triple or quadruple space
$content_block = preg_replace('/[\n\r]+/m',"\n",$content_block);
$content_block = '<p>' . preg_replace('/[\n\r]+/m','</p><p>',$content_block) . '</p>';
// For the last ones on the end, what can I say, got tired and patched it . . .
$content_block = preg_replace('/<p><\/p>/im','',$content_block);
}
return $content_block;
}
?>

P.S. - this is a PIPE character, not a broken line. ¦ it just looks like a broken line here. Also add like 4 extra lines of space in the unformatted example, it will collapse it.

Readie

5:46 am on Jan 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That function works beautifully, so thank you very much :)

There's just one tiny little detail...

I want my users to be able to drop down a single line if they so choose. The function you provided forces the appearance of having dropped down 2 lines under all circumstances - which whilest being a drastic improvement over a mysql error (:D) is still not quite ideal.