Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>According to <a href="http://api.cakephp.org/2.3/class-Sanitize.html#_clean" rel="nofollow">the docs</a>, the <code>clean</code> method does not do what you want.</p> <p>First, for the <code>\n</code> characters, you should call another Sanitize function. <code>Sanitize::clean()</code> has a <code>carriage</code> option, but that only removes the <code>\r</code> characters. So, after (or before) calling the <code>clean</code> method, call <a href="http://api.cakephp.org/2.3/source-class-Sanitize.html#124-132" rel="nofollow">stripWhitespaces</a>. Unfortunately, this functions only receives a string, so you need to call it in a loop (or if you know the string you want to sanitize, use it for just that).</p> <pre><code>$cleanOfSpacesString = Sanitize::stripWhitespace($dirtyString); </code></pre> <p>That function removes this characters: <code>\r</code>, <code>\n</code> and <code>\t</code>, and replace 2 or more spaces with just one.</p> <p>And for <code>&amp;</code>, the docs says that <code>remove_html</code> removes html tags and also does a <code>htmlentities($string, ENT_QUOTES, $defaultCharset, true)</code> to the string. So if that <code>htmlentities</code> is not working for you, you'll have to use another function not incuded in the <code>Sanitize</code> class.</p> <p>If you think this behaviour is something you want to have inside the helper, extend the Sanitize helper to include <code>stripWhiteSpaces</code> inside the <code>clean</code> function and to also replace the <code>htmlentities</code> function with something that works for you. If it's just for this case, add those functions in the controller, after calling <code>Sanitize::clean</code></p> <hr> <p><strong>Update as per Jason's update to my answer</strong></p> <p>I found it weird that the <code>stripWhitespaces</code> function didn't work as you wanted. And here is the explanation of why I think it didn't.</p> <p>You want to remove <code>\n</code> from a string like this </p> <pre><code>'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' </code></pre> <p>and the preg_replace inside stripWhitespaces is this</p> <pre><code>preg_replace('/[\n\r\t]+/', '', $str) </code></pre> <p>That regex <em>will</em> remove newlines, returns and tabs, <em>but</em> on a string like this</p> <pre><code>'test test test ' //and a lot more newlines </code></pre> <p>So the difference here is that you're passing the newlines (maybe the tabs and returns also, I don't know) as <em>strings</em>, real<code>\</code>+<code>n</code>, and so, the <code>preg_replace</code> cake does is not useful for you. I know you found a solution for that (I recommend you add the tab and return match also in there), but I wanted to clarify <em>why</em> cake's function didn't work in case someone else has a similar problem.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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