Forum Moderators: coopster

Message Too Old, No Replies

Textboxes and flatfile databases

         

sloth9

11:04 pm on Oct 27, 2010 (gmt 0)

10+ Year Member



Hi,

So I'm trying to create a flat file database to use on my website.

Basically I fill out a form and a new line is created in a text file separating all the values with ||.

ie.

1||project1||2002||2003||1||0||0||20||19||some project or something||

My problem is this:

The way I use the data (recalled from the text file into an array), every entry must be contained completely on one line in the text file.

Now, the last field is a text box. This input may involve multiple paragraphs.

I need to condense this multiline text into one line of text.

I've tried:

$pdesc=str_replace("\r", "<br>", $pdesc);

but that just returns something like this:

line1<br>
line2<br>
line3

I also got the same result searching for "\n"

The desired result is:

line1<br>line2<br>line3

Does anyone know where I am going wrong or how I can do this?

Thanks in advance.

Zipper

5:58 am on Oct 28, 2010 (gmt 0)

10+ Year Member



try \r\n or nl2br()

astupidname

8:12 am on Oct 28, 2010 (gmt 0)

10+ Year Member



Allow for returns being either there or not, use preg_replace:
$pdesc = preg_replace("/\r?\n/", "<br>", $pdesc);

enigma1

9:37 am on Oct 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could treat the table fields as arrays and serialize them or do a json_encode. Because text may include the '|' character also which may cause trouble trying to identify the fields.

rocknbil

4:22 pm on Oct 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The definition of a carriage return may differ from OS to OS, for example, they are different for Windows and 'nix. A preg will probably server you better.

$pdesc=preg_replace('/[\r\n]+/s', '<br>', $pdesc);

The difference is that the character class [\r\n] will replace either of these, in any order or combined, where with str_replace you're looking for a specific character. If you want to preserve the line breaks, when storing it do

$pdesc=preg_replace('/[\r\n]+/s', '^', $pdesc);

then extract and output,

$pdesc=preg_replace('/^/', "\n", $pdesc);

Of course in both cases you have to be sure to eliminate | or ^ from any input. One approach, if you need to preserve them, is to replace them with their encoded equivalents, then swap those back on extracting from your database.

sloth9

8:29 am on Oct 29, 2010 (gmt 0)

10+ Year Member



Thanks, it worked like a charm. One thing though, could you explain the syntax you used for the pattern argument? I'm not sure I fully understand what is going on there.

Thanks Bil, you rock!

rocknbil

4:06 pm on Oct 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[set some string to ] = preg_replace('[delimiter]pattern[delimiter][modifiers]', '[replace with]', [source string]);

The pattern:

/ = delimiter. Regexps require delimiters, they can be any character (for some reason a lot of PHP coders like to use #)

[] = character class. It means "match on anything inside these brackets." So [abc] would match on anything that contains a, b, or c. [0-9] and [\d] are synonymous, and match on any digit. The interesting thing about 0-9 is the dash, it defines a range. So [0-5] would only match on zero through five, [a-e] would only match on a, b, c, d, or e. Inversely, when ^ is used in a character class, it means anything NOT these characters. So

if (preg_match('/[^\d]+/',$somestring)) {
die ("$somestring contains one or more non-digits.");
}

Which brings us to ...

+ = one or more of the preceding pattern.

s = modifer, it modifies how the match is done, in particular, s insures the match includes newlines which is what you want here. Another common one is i, to match case insensitivity (which you don't need.) As an afternote, m (multiline) may also be useful here (added below.)

preg modifiers [php.net]

So all together,

'/[\r\n]+/sm'

match on one or more newlines, whether they are defined by \r or \n or both, and don't ignore newlines in the match.