Jump to content

get part from text


ashraf5000

Recommended Posts

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

Link to comment
Share on other sites

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.

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.