Forum Moderators: coopster

Message Too Old, No Replies

Trouble appending data onto the end of form data

trying to append "-" to the end of each line from form data.

         

erikcw

5:30 am on May 25, 2004 (gmt 0)

10+ Year Member



I have a textarea in an html form. Users list items in it.

item1
item2
item3

I then process the form data in php so that a "-" is appened at the beginning and end of each line.
-item1-

The problem is that php keeps add a "\n" before the ending "-"

-item1
-
-item2
-
-item3
-

Is there like a "strip\n() function - similar to srtipslashes()?

What should I do?

Here is my code:

echo addMinus();

$word = $_POST['word']; //data from form textarea

function addMinus() {
extract($GLOBALS);

// Read keywords into array.
$word_array = explode("\n", $word);

// Remove empty array keys
$i = 0;
while($i <= count($word_array)) {
if ($word_array[$i] == NULL) {
unset($word_array[$i]);
}
$i++;
}

// process array - add -
$i = 0;
while($i < count($word_array)) {
$temp .= $word_array[$i] . "\n";
$i++;
}
$i = 0;
while($i < count($word_array)) {
$temp .= '-' . $word_array[$i] . '-' . "\n";
$i++;
}

return $temp;

}

NickCoons

5:50 am on May 25, 2004 (gmt 0)

10+ Year Member



erikcw,

<Is there like a "strip\n() function - similar to srtipslashes()?>

Yes.. trim().

dcrombie

10:07 am on May 25, 2004 (gmt 0)



Maybe this will help;

$PATTERNS = array("/(^¦$)/", "/[\r\n]+/"); 
$REPLACEMENTS = array("-", "-\n-");
$output = preg_replace($PATTERNS, $REPLACEMENTS, $input);

;)

erikcw

4:43 pm on May 25, 2004 (gmt 0)

10+ Year Member



When I try to use the regular expressions you offered, I get:

test-
-test3-
-sdf3-
-

So it is a little close, but for some reason I am getting a "-" and then very end on its own line instead of before the first word. Any ideas?

dcrombie

4:51 pm on May 25, 2004 (gmt 0)



Did you replace the '¦' with a proper '¦'?
(this forum mangles the pipe character)

erikcw

5:00 pm on May 25, 2004 (gmt 0)

10+ Year Member



Ok, I fixed the ¦ character. But now it is appeneding extra data.

-terw-
-tesdf-
-ewrtw-
-fgsdf-
---
--

Where are these extra "-" coming from?

dcrombie

5:26 pm on May 25, 2004 (gmt 0)



It seems your input has some extra line breaks at the end. All you need to do is replace $input with trim($input) in the code I pasted above.

erikcw

5:47 pm on May 25, 2004 (gmt 0)

10+ Year Member



dcrombie, thank you so much for all of your help! That cleared up the problem with the extra spaces and the "-" are printing out fine.

I also needed to print a second version surrounded by {} instead of --


$PATTERNS = array("/(^¦$)/", "/[\r\n]+/");
$REPLACEMENTS = array("{", "}\n{");
$output = preg_replace($PATTERNS, $REPLACEMENTS, trim($temp));

The problem is that it is print the wrong bracket on the last piece of output:

{test}
{tefs3}
{dfds43{

I'm guessing it has something to do with how things are layed out in $REPLACEMENTS but I don't know how to switch the bracket so it prints correctly.

I swear this is the last problem! ;-)

dcrombie

11:26 am on May 26, 2004 (gmt 0)



Option 1:

$PATTERNS = array("/^/", "/$/", "/[\r\n]+/");
$REPLACEMENTS = array("{", "}", "}\n{");
$output = preg_replace [php.net]($PATTERNS, $REPLACEMENTS, trim($input));

Option 2:

$PATTERNS = array("/^/m", "/$/m");
$REPLACEMENTS = array("{", "}");
$output = preg_replace [php.net]($PATTERNS, $REPLACEMENTS, trim($input));

erikcw

6:20 pm on May 26, 2004 (gmt 0)

10+ Year Member



Option #1 did exactly the trick! Thanks for all your help!

Erik