Trim doesn't remove \n - php

I am taking a string from textarea and exploding it and trimming each line of the array with array_map():
$answers = explode("\n", $data['answers']);
// remove all whitespace such as \r (carriage return)
$asnwers = array_map('trim', $answers);
Then I store each array value in a separate row in a table answers in the database. The problem is there seems to be \n character at the end of each answer in the database. When I echo answers in HTML like this:
<?php foreach ($this->answers as $a): ?>
<tr>
<td><?php echo $this->escape($a->body); ?></td>
</tr>
<?php endforreach; ?>
When I then look at the HTML source I see this:
<tr>
<td>Some random answer
</td>
</tr>
As you can see, there is a newline (probably \n) at the end of the string because the closing tag gets moved to the next line.
What I am doing wrong?

$asnwers = array_map('trim', $answers);
You're assigning the return value of array_map to $asnwers. It should be $answers.

Related

convert string fetched with php nl2dr into array [duplicate]

I have form in html where user can put the text in text area.
I save the content of text area into MySQL database (in field of type TEXT).
Then I somewhere in my aplication I need load that text and put it into array where in each index will be one line of the text.
<textarea rows="10" cols="80">
Row one
Row two
Row tree
</textarea>
array (output in "php pseudocode"):
$array = array();
$array[0] = Row one;
$array[1] = Row two;
$array[2] = Row tree;
How I do it:
I save it to db then I load it and use:
$br = nl2br($string,true);
$array = explode("<br />", $br);
The reason why I use nl2br is I want to avoid problems with end of lines of the text from different platforms. I mean /n or /r/n.
But in my solution must be an error somewhere cause it doesn't work (an array $array is empty).
Better solution would be probably somehow split it into array using explode or something like that with pattern for line breaks but here is again the problem from beginning with line breaks which I don't know how to solve (problems with \n or \r\n).
Can anybody give me an advice? Thanks.
I'd suggest you skip the nl2br until you're ready to actually send the data to the client. To tackle your problem:
// $string = $get->data->somehow();
$array = preg_split('/\n|\r\n/', $string);
When you receive the input in your script normalize the end-of-line characters to PHP_EOL before storing the data in the data base. This tested out correctly for me. It's just one more step in the "filter input" process. You may find other EOL character strings, but these are the most common.
<?php // RAY_temp_user109.php
error_reporting(E_ALL);
if (!empty($_POST['t']))
{
// NORMALIZE THE END-OF-LINE CHARACTERS
$t = str_replace("\r\n", PHP_EOL, $_POST['t']);
$t = str_replace("\r", PHP_EOL, $t);
$t = str_replace("\n", PHP_EOL, $t);
$a = explode(PHP_EOL, $t);
print_r($a);
}
$form = <<<FORM
<form method="post">
<textarea name="t"></textarea>
<input type="submit" />
</form>
FORM;
echo $form;
Explode on PHP_EOL and skip the nol2br() part. You can use var_dump() to see the resulting array.

Handling new lines in php

I have form in html where user can put the text in text area.
I save the content of text area into MySQL database (in field of type TEXT).
Then I somewhere in my aplication I need load that text and put it into array where in each index will be one line of the text.
<textarea rows="10" cols="80">
Row one
Row two
Row tree
</textarea>
array (output in "php pseudocode"):
$array = array();
$array[0] = Row one;
$array[1] = Row two;
$array[2] = Row tree;
How I do it:
I save it to db then I load it and use:
$br = nl2br($string,true);
$array = explode("<br />", $br);
The reason why I use nl2br is I want to avoid problems with end of lines of the text from different platforms. I mean /n or /r/n.
But in my solution must be an error somewhere cause it doesn't work (an array $array is empty).
Better solution would be probably somehow split it into array using explode or something like that with pattern for line breaks but here is again the problem from beginning with line breaks which I don't know how to solve (problems with \n or \r\n).
Can anybody give me an advice? Thanks.
I'd suggest you skip the nl2br until you're ready to actually send the data to the client. To tackle your problem:
// $string = $get->data->somehow();
$array = preg_split('/\n|\r\n/', $string);
When you receive the input in your script normalize the end-of-line characters to PHP_EOL before storing the data in the data base. This tested out correctly for me. It's just one more step in the "filter input" process. You may find other EOL character strings, but these are the most common.
<?php // RAY_temp_user109.php
error_reporting(E_ALL);
if (!empty($_POST['t']))
{
// NORMALIZE THE END-OF-LINE CHARACTERS
$t = str_replace("\r\n", PHP_EOL, $_POST['t']);
$t = str_replace("\r", PHP_EOL, $t);
$t = str_replace("\n", PHP_EOL, $t);
$a = explode(PHP_EOL, $t);
print_r($a);
}
$form = <<<FORM
<form method="post">
<textarea name="t"></textarea>
<input type="submit" />
</form>
FORM;
echo $form;
Explode on PHP_EOL and skip the nol2br() part. You can use var_dump() to see the resulting array.

Can I use conditional logic in PHP EOD?

Is it possible to put conditional logic inside an EOD string?
$str = <<<EOD
<table>
<tr>
<td>
if ( !empty($var1) ) {
{$var1}
} else {
{$var2}
}
</td>
</tr>
</table>
This doesn't work for me, and it sort of looks like it wouldn't work, but I thought I'd take a stab.
Also, is it EOD or EOT? Both seem to work.
No. You cannot use conditionals in heredoc.
Also, is it EOD or EOT?
As long as your beginning and ending strings match you can use anything:
$x = <<<THOMAS
Pick a string, any string
THOMAS;
The doc contains several examples demonstrating this
As to how best to achieve the example you provided, this would be my first inclination:
$td = !empty($var1) ? $var1 : $var2;
$str = <<<EOD
<table>
<tr>
<td>
{$td}
</td>
</tr>
</table>
EOD;

How would one strip text before and after a certain html tag

Basically I want to take something like:
textheretextheretexthere
<table>
<tr><td>heres some stuff</td><td>and some morestuff</td></tr>
</table>
moretextheremoretexthere
and remove all the texthere and moretext here just leaving the table
You need to find <table> position with strpos and then use substr to remove the text to this point.and then the same for </table>
$string = 'textheretextheretexthere<table><tr><td>heres some stuff</td><td>and some morestuff</td></tr></table>moretextheremoretexthere';
$table_pos = strpos($string,'<table>');
$string = substr($string,$table_pos);
//Your string now is <table><tr><td>heres some stuff</td><td>and some morestuff</td></tr></table>moretextheremoretexthere
$endtable_pos = strpos($string,'</table>')+8;//added 8 so i wont exclude </table>
$clean_string = substr($string,0,$endtable_pos);
//Your string now is <table><tr><td>heres some stuff</td><td>and some morestuff</td></tr></table>
Of course this is not perfect at all,i know but you got the hint,you can work on improving it and maybe end up with a function that helps you solve your problem.
strpos
substr
<?php
$text = '<p>Test paragraph.</p><!-- Comment --> Other text';
echo strip_tags($text);
echo "\n";
// Allow <p> and <a>
echo strip_tags($text, '<p><a>');
?>
This is would be the easiest way.
http://yelotofu.com/2008/08/jquery-outerhtml/

Using nl2br with html tags

I use nl2br when displaying some information that is saved somewhere, but when HTML tags are used I want not to add <br> tags for them.
For example if I use
<table>
<th></th>
</table>
it will be transformed to
<table><br />
<th></th><br />
</table><br />
and that makes a lot of spaces for this table.
Ho can break line tags be added only for other non-HTML content?
Thanks.
I'd the same issue,
I made this code, adding a <br /> at the end of each line except if the line finished with an html tag:
function nl2br_save_html($string)
{
if(! preg_match("#</.*>#", $string)) // avoid looping if no tags in the string.
return nl2br($string);
$string = str_replace(array("\r\n", "\r", "\n"), "\n", $string);
$lines=explode("\n", $string);
$output='';
foreach($lines as $line)
{
$line = rtrim($line);
if(! preg_match("#</?[^/<>]*>$#", $line)) // See if the line finished with has an html opening or closing tag
$line .= '<br />';
$output .= $line . "\n";
}
return $output;
}
You could replace the closing tags and newlines by only closing tags:
$str = str_replace('>
', '>', $str);
I think your question is wrong. If you are typing
<table>
<th></th>
</table>
into a text area then no matter what you do It will include <br /> in between them. Because it is what nl2br is supposed to do.

Categories