0

I am trying to replace all values of array except one key value. I am using preg_replace and have this code:

$posts = json_decode($post_list, true);
        foreach($posts as &$elm){
            $elm = array_map(function($i) use($s){
                return preg_replace("/(" . preg_quote($s) . ")/is", "<span style='background: #92CF18;'>$1</span>", $i);
            }, $elm);
        }

My posts var looks like this: pastebin I want to don't change value of "slug" key how can I do it?

Zolax
  • 91
  • 3
  • 12
  • What's the value of `$s`? Show the actual regex you're using. – Bogdan Nov 29 '14 at 20:26
  • $s is input that comes from user It's my search results sys. That color text you're looking for. It's all what I have already. I'm stuck with this problem. – Zolax Nov 29 '14 at 20:34

1 Answers1

0

I recommend that you do not modify your json data object with markup. Data is a different concern then markup.

Markup should be on the rendered output, so your step should be jQuery/javascript on the resulting markup.

In the javascript, you should then check for 'slug' and early return on it. After that, the substitution is probably trivial, or at least easier than my earlier answer:

If I understand correctly, you want to have all keys in your dictionary per record in your array, except the slug key.

I twiddled a bit with it, using negative lookahead, and came to this solution:

(\{|,)\"(?!slug).*?\"

You can check it at: http://regex101.com/r/bU9bL4/1

If this does not completely satify your needs, please provide a better description of the modification you want, including some examples.

Pieter21
  • 1,765
  • 1
  • 10
  • 22
  • I want to replace all keys that are equals to $s var. $s is user input in search form. Can you show me how should I change my statement? – Zolax Nov 29 '14 at 21:10