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
https://forums.phpfreaks.com/topic/254844-complex-string-replacement/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.