Note that there are some explanatory texts on larger screens.

plurals
  1. POCakePHP - Sanitize::clean leaving &amp and \n
    text
    copied!<p>I'm getting a little confused with what exactly cakePHPs Sanitize::clean() method should do. Currently when I'm adding a record I'm doing this:</p> <pre><code>$this-&gt;request-&gt;data = Sanitize::clean($this-&gt;request-&gt;data, array('encode' =&gt; true, 'remove_html' =&gt; true)); </code></pre> <p>However, this still leaves &amp; and \n in my database when they use &amp; and press enter in a textarea. How can I stop this? I thought remove_html => true would have done this?</p> <p>Do I need to go as far as doing a str_replace()</p> <p>Also some of the records with the \n 's in them have hundreds of trailing backslashes meaning the break any views they are displayed on.</p> <p>Could someone point me in the right direction? Thanks</p> <p><strong>Update as per Nunsers answer</strong> </p> <p>I've now added the following after the clean:</p> <pre><code>foreach ($this-&gt;request-&gt;data['Expense'] as &amp;$expense) { $expense['detail'] = Sanitize::stripWhitespace($expense['detail']); } unset($expense); </code></pre> <p>However, it does remove whitespace but still leaves lots of <code>\n\n\n\n\n\</code></p> <p>Heres a debug of $this->request->data:</p> <pre><code>array( 'submit' =&gt; 'Save', 'Expense' =&gt; array( (int) 0 =&gt; array( 'date' =&gt; array( 'day' =&gt; '27', 'month' =&gt; '06', 'year' =&gt; '2013' ), 'sitename' =&gt; 'test', 'detail' =&gt; 'test test\n\ntest\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n', 'ExpenseCode' =&gt; array( 'ExpenseCode' =&gt; '1' ), 'billable' =&gt; '1', 'amount' =&gt; '1', 'miles' =&gt; '', 'total' =&gt; '1', 'billable_mileage' =&gt; '', 'recorded_global_mileage_rate' =&gt; '0.4' ), (int) 1 =&gt; array( 'date' =&gt; array( 'day' =&gt; '27', 'month' =&gt; '06', 'year' =&gt; '2013' ), 'sitename' =&gt; 'test', 'detail' =&gt; '\n\n\n\n\n\n\n\n\n\n\n\n\n\ntest', 'ExpenseCode' =&gt; array( 'ExpenseCode' =&gt; '4' ), 'billable' =&gt; '1', 'amount' =&gt; '10', 'miles' =&gt; '', 'total' =&gt; '10', 'billable_mileage' =&gt; '', 'recorded_global_mileage_rate' =&gt; '0.4' ) ), 'CashFloat' =&gt; array( 'amount' =&gt; '' ), 'ExpenseClaim' =&gt; array( 'user_id' =&gt; '3', 'claim_status_id' =&gt; '1' ) ) </code></pre> <p>I'd like to strip thouse \n's out really as I dont want them break views.</p> <p><strong>More results</strong></p> <p>Even when I cut out the cake function and use its code directly inline via :</p> <pre><code>$expense['detail'] = preg_replace('/\s{2,}/u', ' ', preg_replace('/[\n\r\t]+/', '', $expense['detail'])); </code></pre> <p>I still get the same (debug($expense['detail']) from the loop:</p> <pre><code>'test 10 spaces before this then pressing enter lots \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n' </code></pre> <p>I've also tried just a basic trim() which didnt work at all.</p> <p><strong>Working solution (apart from the &amp;)</strong></p> <p>This will remove any number of </p> <pre><code>\n </code></pre> <p>from the string</p> <pre><code>foreach ($this-&gt;request-&gt;data['Expense'] as &amp;$expense) { $expense['detail'] = str_replace("\\n", ' ', $expense['detail']); $expense['detail'] = Sanitize::stripWhitespace($expense['detail']); } // Unset referance var from above loop unset($expense); </code></pre> <p><strong>Decided to keep the &amp;</strong></p> <p>And just use <code>html_entity_decode()</code> when echoing it out in a view</p> <p>Hope that helps someone!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload