开发者

str_ireplace() not accutally removing the 'needles'

I'm using str_irepl开发者_开发知识库ace() to remove instances of strings in an array, and I'm returning the number of counted occurances, but it's not actually performing the replace.

//replace occurances of insert, update, delete, select
$dmlArray = array('select', 'update', 'delete', 'insert');

str_ireplace($dmlArray,'-- replaced DML -- ',$clean['comment'],$Incount);

Where $clean['comment'] would be the $_POST array.

For example, $clean['comment'] = "SELECT, insert, UPDATE, DEleTe";

The final string should be "-- replaced DML -- ,-- replaced DML -- ,-- replaced DML -- ,-- replaced DML -- ";

Yet it's not.


Function str_ireplace doesn't change its arguments. It returns the result. Here is fix for your code:

$clean['comment'] = str_ireplace($dmlArray, '-- replaced -- ', $clean['comment'], $Incount);


I've run your code and it works as expected.

echo str_ireplace(
    array('select', 'update', 'delete', 'insert'),
    '-- replaced DML -- ',
    'SELECT, insert, UPDATE, DEleTe',
    $Incount);

The above will output

-- replaced DML -- , -- replaced DML -- , -- replaced DML -- , -- replaced DML --

Just keep in mind that the input string is not passed in by reference, so you have to use the return value to get the string with the values replaced.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜