-1

I have this kind of string.

'"asdfasdf","123456", this is a message. OK'

What I want to do is declare variables according to the first and second quoted values and then the rest of the message until the OK. (note: the length of the string inside the '' is not consistent)

$First = "asdfasdf"
$Second = "123456"
$Message = "this is a message"

Is this even possible? Is there something like " "$First","$Second", "$Message" OK " kind of way?

3
  • 1
    there are several ways to achieve this, for example regular expressions Commented Dec 28, 2014 at 5:25
  • @Lashane can you please give me a link to learn your example. i keep finding rubbish in google. thanks. Commented Dec 28, 2014 at 5:29
  • en.wikipedia.org/wiki/Regular_expression Commented Dec 28, 2014 at 5:31

3 Answers 3

1

Is this a CSV file ? Doesn't seem to, but if it was you should check out the csv functions of php, specifically str_getcsv.

If not, you should just do an explode by , or ", or any combination you think would be most accurate, and then go through each array item.

$string = '"asdfasdf","123456","this is a message. OK"';
$temp = explode('","',$string);
$array = array();
foreach($temp as $key=>$value){
//do stuff with $value and $key
}
Sign up to request clarification or add additional context in comments.

2 Comments

instead of foreach-ing $temp, you can use list, may be a little faster if you only want to define some variables.
I'm thinking this might encounter a problem, for when the message will contain the symbol ",". Thanks this gave me an idea.
0

You can use regular expressions, like this:

Code

$raw = '"asdfasdf","123456", this is a message. OK'; // this is your raw text
preg_match('/^"(?P<first>[^"]+)","(?P<second>[^"]+)",\s+(?P<message>.+?) OK/', $raw, $matches); // this looks for the pattern you defined and stores the matches in $matches
print_r($matches); // this just dumps out the array of matching substrings

Output

Array
(
    [0] => "asdfasdf","123456", this is a message. OK
    [first] => asdfasdf
    [1] => asdfasdf
    [second] => 123456
    [2] => 123456
    [message] => this is a message.
    [3] => this is a message.
)

You can access the individual substrings as, for example, $matches['first'], $matches['second'], or $matches['message'].

PHP Demo

Regex Demo

3 Comments

Thank you very much this is pretty much what i needed since the format is the same.
Sir, Can you help me please? I'm getting error with this one.I want to get the number,date,time,and message. I read the response from gsm modem using "fread" and I store it which result to $response='+CMGR: "REC READ","+639178397864",,"11/11/13,21:04:38+32"Hello WOrldOK'
This is a little trickier. You can try changing \s+ above to \s* and seeing if that does what you want. If not, please post this as a new question using the Ask Question button. You can link to this question to provide context.
0

Parse your perfectly valid CSV string with str_getcsv() as the PHP gods intended, then use symmetric array destructuring to assign values to variables.

Code: (Demo)

[$first, $second, $message] = str_getcsv('"asdfasdf","123456", this is a message. OK');
var_dump($first, $second, $message);

Output:

string(8) "asdfasdf"
string(6) "123456"
string(22) " this is a message. OK"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.