【问题标题】:CakePHP - Sanitize::clean leaving &amp and \nCakePHP - Sanitize::clean 离开 &amp 和 \n
【发布时间】:2013-06-26 15:42:59
【问题描述】:

我对 cakePHPs Sanitize::clean() 方法到底应该做什么有点困惑。目前,当我添加记录时,我正在这样做:

$this->request->data = Sanitize::clean($this->request->data, array('encode' => true, 'remove_html' => true));

但是,当他们使用 & 并在文本区域中按 enter 时,这仍然会在我的数据库中留下 & 和 \n。我怎样才能阻止这个?我认为 remove_html => true 会这样做吗?

我需要做一个 str_replace() 吗

还有一些带有 \n 的记录有数百个尾随反斜杠,这意味着它们显示的任何视图都会中断。

有人能指出我正确的方向吗?谢谢

根据修女回答更新

我现在在清理之后添加了以下内容:

foreach ($this->request->data['Expense'] as &$expense) {
    $expense['detail'] = Sanitize::stripWhitespace($expense['detail']);         
}
unset($expense);

但是,它确实会删除空格,但仍然会留下很多 \n\n\n\n\n\

这是 $this->request->data 的调试:

array(
    'submit' => 'Save',
    'Expense' => array(
        (int) 0 => array(
            'date' => array(
                'day' => '27',
                'month' => '06',
                'year' => '2013'
            ),
            'sitename' => 'test',
            'detail' => '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' => array(
                'ExpenseCode' => '1'
            ),
            'billable' => '1',
            'amount' => '1',
            'miles' => '',
            'total' => '1',
            'billable_mileage' => '',
            'recorded_global_mileage_rate' => '0.4'
        ),
        (int) 1 => array(
            'date' => array(
                'day' => '27',
                'month' => '06',
                'year' => '2013'
            ),
            'sitename' => 'test',
            'detail' => '\n\n\n\n\n\n\n\n\n\n\n\n\n\ntest',
            'ExpenseCode' => array(
                'ExpenseCode' => '4'
            ),
            'billable' => '1',
            'amount' => '10',
            'miles' => '',
            'total' => '10',
            'billable_mileage' => '',
            'recorded_global_mileage_rate' => '0.4'
        )
    ),
    'CashFloat' => array(
        'amount' => ''
    ),
    'ExpenseClaim' => array(
        'user_id' => '3',
        'claim_status_id' => '1'
    )
)

我真的想把 thouse\n 去掉,因为我不希望它们破坏视图。

更多结果

即使我删除了 cake 函数并直接通过内联使用它的代码:

$expense['detail'] = preg_replace('/\s{2,}/u', ' ', preg_replace('/[\n\r\t]+/', '', $expense['detail']));

我仍然从循环中得到相同的 (debug($expense['detail']):

'test 10 spaces before this then pressing enter lots \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n'

我也尝试了一个基本的 trim(),但根本不起作用。

工作解决方案(除了 &)

这将删除任意数量的

\n

从字符串

foreach ($this->request->data['Expense'] as &$expense) {

    $expense['detail'] = str_replace("\\n", ' ', $expense['detail']);
    $expense['detail'] = Sanitize::stripWhitespace($expense['detail']);             
}
// Unset referance var from above loop
unset($expense);

决定保留&

在视图中回显时只需使用html_entity_decode()

希望对某人有所帮助!

【问题讨论】:

    标签: cakephp sanitization


    【解决方案1】:

    根据the docsclean 方法不能做你想做的事情。

    首先,对于 \n 字符,您应该调用另一个 Sanitize 函数。 Sanitize::clean() 有一个 carriage 选项,但这只会删除 \r 字符。因此,在调用clean 方法之后(或之前),调用stripWhitespaces。不幸的是,这个函数只接收一个字符串,所以你需要在循环中调用它(或者如果你知道要清理的字符串,就用它)。

    $cleanOfSpacesString = Sanitize::stripWhitespace($dirtyString);
    

    该函数会删除以下字符:\r\n\t,并将 2 个或多个空格替换为一个。

    对于&,文档说remove_html 删除了html 标记并对字符串执行htmlentities($string, ENT_QUOTES, $defaultCharset, true)。因此,如果 htmlentities 不适合您,您将不得不使用 Sanitize 类中未包含的另一个函数。

    如果您认为此行为是您希望在帮助程序中包含的内容,请扩展 Sanitize 帮助程序以将 stripWhiteSpaces 包含在 clean 函数中,并将 htmlentities 函数替换为适合您的函数。如果只是针对这种情况,请在控制器中添加这些功能,在调用Sanitize::clean之后


    根据 Jason 对我的回答的更新进行更新

    我发现 stripWhitespaces 函数没有按你想要的那样工作很奇怪。这是我认为没有的原因的解释。

    你想从这样的字符串中删除\n

    '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'
    

    stripWhitespaces 里面的 preg_replace 就是这个

    preg_replace('/[\n\r\t]+/', '', $str)
    

    那个正则表达式删除换行符、回车符和制表符,但是在这样的字符串上

    'test test
    
    test
    
    ' //and a lot more newlines
    

    所以这里的不同之处在于您将换行符(也许是制表符和返回,我不知道)作为 strings、real\+n 等等,preg_replace 蛋糕对你没用。我知道您找到了解决方案(我建议您在其中添加选项卡并返回匹配项),但我想澄清 为什么 cake 的功能在其他人遇到类似问题的情况下不起作用.

    【讨论】:

    • 嗨,谢谢你的回答——它现在去掉了空格,但留下了 \n 我已经用更多细节更新了我的问题——你介意看一下吗?谢谢
    • 我已经更新了我的答案,我认为这是您的\n 被忽略的原因:)
    猜你喜欢
    • 1970-01-01
    • 2013-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-04
    • 2020-08-20
    相关资源
    最近更新 更多