views:

53

answers:

2

I'm using str_ireplace() 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.

+1  A: 

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.

Gordon
+3  A: 

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);
Ivan Nevostruev
I knew I was forgetting something simple, thanks!
Michael Stone