Jump to content

Complex string replacement


Ortix

Recommended Posts

Hi guys, I need to find a way to do the following

 

in this string:

{"id":"51","value":["Lorem Ipsum","Lorem Ipsum","new"]}

 

I need to match

"id":"51"

 

If that is matched, I want to remove that ENTIRE string. How would I go about doing that?

 

In other words, I run a preg_match or something like that and if "id":"51" is found, i need to remove the entire element in which it is contained. In this case the element is the above mentioned string.

 

Any ideas? Perhaps with RegEx? (I have no clue how to use RegEx)

Link to comment
Share on other sites

The string you are using is only JSON encode, you can decode it by using json_decode

 

Something like this should work:

<?PHP

  $string = '{"id":"51","value":["Lorem Ipsum","Lorem Ipsum","new"]}';

  $decodeStr = json_decode($string,true);

  if($decodeStr['id'] == 51) {
    $string = '';
  }

  echo $string;

?>

 

Regards, PaulRyan.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.