Trying to manipulate a string

#1

Hi all,

I have a string which looks like the following

“Date”:“Tuesday, June 18, 2019”,“Notes”:"» None",“Detail”:" \n » Members Required: …etc

It’s part of a JSON which I am trying to manipulate so this happens multiple times within the file.

I am ideally looking for something PHP that says: Everytime I see “Detail”:" ditch the 4 characters after it, rinse and repeat for the rest of the file.

I’m basically trying to get rid of the \n new line but there are other lines I need to keep in the rest of the file so I can’t really do a str_replace for all \n.

I thought of a str_replace i.e.:

str_replace(’“Detail”:" \n »’,’“Detail”:" »’,$content);

but i can’t get that to work at all.

Any help would be appreciated.

Thanks,
Chris

#2

As your task shows, string operations are mostly a false approach on structured data, so i would recommend to use the ‘RecursiveArrayIterator’ or implement plain array recursion by yourself.

https://www.php.net/manual/en/class.recursivearrayiterator.php

#3

Hi Chorn,

Sorry, I have no clue what that is and from quickly watching some youtube on it I ain’t getting very far with it either.

I understand it isn’t pretty but it’s nothing critical. Have I just mis structured my str_replace function that I thought should work or will it not work for some reason unknown to me?

Thanks,
Chris

#4

You already pointed out your main problem

string operations are insufficient because they don’t respect the context - you need a parser.

#5

eh, without more information, I’d use trim()