ashraf5000 Posted April 29, 2020 Share Posted April 29, 2020 Hello, I am new to the php community I have a problem with feedback code from api I want to put a part in a variable How is that ? please help {"code": 0, "reason": "OK", "resp": "AT + CCFC = 1,3, \" 92061460 \ "\ r \ nOK \ r \ n"} I want to put between \ r \ Variable \ r \ In this case is == nOK some time {"code":0, "reason":"OK", "resp":"AT+CCFC=1,3,\" 92061460\"\r\nERROR\r\n"} in this case = nERROR need echo $varible i hope get your support Quote Link to comment Share on other sites More sharing options...
gw1500se Posted April 29, 2020 Share Posted April 29, 2020 It is a bit hard to understand what you want but I think you are looking for str_replace. Quote Link to comment Share on other sites More sharing options...
ashraf5000 Posted April 30, 2020 Author Share Posted April 30, 2020 thanks for your replay i need delete all text else text between \ r \ xxxxxxxx \ r \ in this case nOK i need feed back nOK or nERROR i think we can use explode but i'am new in php and don't how do it Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted April 30, 2020 Share Posted April 30, 2020 The following line looks like JSON. {"code":0, "reason":"OK", "resp":"AT+CCFC=1,3,\" 92061460\"\r\nERROR\r\n"} You could try using PHP's json_decode() function to extract the code, reason, and resp variables. More information, including examples, can be found in the manual.https://www.php.net/manual/en/function.json-decode.php Then you could use explode() to break the "resp" component based off the double quote ("). Then chop off the third value, assuming the lines always look similar to the above. Does the following line come from the API? {"code": 0, "reason": "OK", "resp": "AT + CCFC = 1,3, \" 92061460 \ "\ r \ nOK \ r \ n"} Unless I'm missing something, that's not valid code. Basically, the "resp" portion is equal to "AT + CCFC = 1,3, \" 92061460 \ "\ r \ nOK \ r \ n", which is surrounded by double quotes. In order to use double quotes in the middle of the string, they need to be escaped with a backslash. The first escaped quote is fine. The second one is not since there is a space between the backslash and the quote. The \r and \n entities are also invalid because of the extra spaces. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.