Forum Moderators: coopster
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;
}
$PATTERNS = array("/(^¦$)/", "/[\r\n]+/");
$REPLACEMENTS = array("-", "-\n-");
$output = preg_replace($PATTERNS, $REPLACEMENTS, $input);
;)
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! ;-)
$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));