Skip to main content
Topic: Some opinions (Read 2575 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Some opinions

QuoteA faster way to replace the strings in multidimensional array is to json_encode() it, do the str_replace() and then json_decode() it, like this:

<?php
function str_replace_json($search, $replace, $subject){
     return json_decode(str_replace($search, $replace,  json_encode($subject)));

}
?>

This method is almost 3x faster (in 10000 runs.) than using recursive calling and looping method, and 10x simpler in coding.

Compared to:

<?php
function str_replace_deep($search, $replace, $subject)
{
    if (is_array($subject))
    {
        foreach($subject as &$oneSubject)
            $oneSubject = str_replace_deep($search, $replace, $oneSubject);
        unset($oneSubject);
        return $subject;
    } else {
        return str_replace($search, $replace, $subject);
    }
}
?>
How true is this[1]? Can this work on preg_repalce as well?

Re: Some opinions

Reply #1

That's possible, I never tested it...
Bugs creator.
Features destroyer.
Template killer.

Re: Some opinions

Reply #2

You can very quickly and easily mess up the array. It's not at all safe to do and wouldn't recommend it.

Re: Some opinions

Reply #3

Are you sure of that being not safe? This is the top response and preferred by over 120 readers. I am sure if it is bad they will voted it negatively instead. ::)

By the way, I was merely asking when I posted this. I don't even know how to use it until I really need to use it. :D

Re: Some opinions

Reply #4

Say you replace ' with " or : with a space. Broken JSON

Re: Some opinions

Reply #5

You mean certain few character cannot be string replaced? Or are they a lot more that make it unsafe?

Re: Some opinions

Reply #6

Look up the JSON specs. Any character that is used cannot be replaced. Then anything that gets escaped would also not be able to be replaced because you'd change it. It's just not safe. For the minimal savings it doesn't make sense.

Re: Some opinions

Reply #7

A JSON string is delimited by double quotes.
If you replace a string with one containing a double quote you have to first escape that double quote, otherwise the undecode will not be able to work properly.

Seeing this in context, I'd say that:
1) str_replace on a json string is doable,
1a) it's easier than do it with a serialized array,
2) you have to pay a little attention to what you are replacing with, but this is not much different than replacing other stuff.

From the tests I did, it depends on both the size of the array and the length of the strings in the array.
I think there is not "right" answer, what I can say is:
1) the longer the strings in the array are, the faster str_replace_deep is compared to json replace,
2) the longer the array is (i.e. more elements), the faster tend to be json compared to str_replace_deep (provided the strings are not too long, see point 1).
Bugs creator.
Features destroyer.
Template killer.