Skip to content

Commit f938c09

Browse files
author
Mark Scherer
committed
Fix PHP7 code
1 parent 09bb66e commit f938c09

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

Test/Case/View/Helper/FormatHelperTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -499,14 +499,16 @@ public function testWordCensor() {
499499
'122 jsdf ficken Sjdkf sdfj sdf' => '122 jsdf ###### Sjdkf sdfj sdf',
500500
'122 jsdf FICKEN sjdkf sdfjs sdf' => '122 jsdf ###### sjdkf sdfjs sdf',
501501
'dddddddddd ARSCH ddddddddddddd' => 'dddddddddd ##### ddddddddddddd',
502-
//'\';alert(String.fromCharCode(88,83,83))//\';alert(String.fromCharCode(88,83,83))//";alert(String.fromCharCode(88,83,83))//\";alert(String.fromCharCode(88,83,83))//--></SCRIPT>">\'><SCRIPT>alert(String.fromCharCode(88,83,83))</SCRIPT>' => null
503502
];
504503
foreach ($data as $value => $expected) {
505504
$res = $this->Format->wordCensor($value, ['Arsch', 'Ficken', 'Bitch']);
506-
507-
//debug('\''.h($value).'\' becomes \''.h($res).'\'', null, false);
508505
$this->assertEquals($expected === null ? $value : $expected, $res);
509506
}
507+
508+
$input = 'dfssdfsdj sdkfj sdkfj ksdfj bitch ksdfj';
509+
$result = $this->Format->wordCensor($input, ['Bitch'], '***');
510+
$expected = 'dfssdfsdj sdkfj sdkfj ksdfj *** ksdfj';
511+
$this->assertEquals($expected, $result);
510512
}
511513

512514
/**

View/Helper/FormatHelper.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,9 +1243,9 @@ public function tab2space($text, $spaces = 4) {
12431243
*
12441244
* @todo Move to Text Helper etc.
12451245
*
1246-
* @param string the text string
1247-
* @param string the array of censoered words
1248-
* @param string the optional replacement value
1246+
* @param string $str The text string
1247+
* @param string $censored The array of censoered words
1248+
* @param string|null $replacement The optional replacement value
12491249
* @return string
12501250
*/
12511251
public function wordCensor($str, $censored, $replacement = null) {
@@ -1264,8 +1264,13 @@ public function wordCensor($str, $censored, $replacement = null) {
12641264
if ($replacement !== null) {
12651265
$str = preg_replace("/({$delim})(" . str_replace('\*', '\w*?', preg_quote($badword, '/')) . ")({$delim})/i", "\\1{$replacement}\\3", $str);
12661266
} else {
1267-
$str = preg_replace("/({$delim})(" . str_replace('\*', '\w*?', preg_quote($badword, '/')) . ")({$delim})/ie", "'\\1'.str_repeat('#', strlen('\\2')).'\\3'",
1268-
$str);
1267+
$str = preg_replace_callback(
1268+
"/({$delim})(" . str_replace('\*', '\w*?', preg_quote($badword, '/')) . ")({$delim})/i",
1269+
function ($x) {
1270+
return $x[1] . str_repeat('#', strlen($x[2])) . $x[3];
1271+
},
1272+
$str
1273+
);
12691274
}
12701275
}
12711276

0 commit comments

Comments
 (0)