我有这种字符串。
'"asdfasdf","123456", this is a message. OK'
我想要做的是根据第一个,第二个引号和消息的其余部分声明变量,直到OK ...
(注意:''
内部字符串的长度不一致)
$First = "asdfasdf"
$Second = "123456"
$Message = "this is a message"
这甚至可能吗?
有类似" "$First","$Second", "$Message" OK "
的方式吗?
TIA。
答案 0 :(得分:2)
这是CSV文件吗? 似乎没有,但如果是,你应该查看php的csv函数,特别是str_getcsv。
如果没有,你应该只是做一个爆炸,或者,或者你认为最准确的任何组合,然后遍历每个数组项。
$string = '"asdfasdf","123456","this is a message. OK"';
$temp = explode('","',$string);
$array = array();
foreach($temp as $key=>$value){
//do stuff with $value and $key
}
答案 1 :(得分:0)
您可以使用正则表达式,如下所示:
<强>代码强>
$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
<强>输出强>
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.
)
您可以访问各个子字符串,例如$matches['first']
,$matches['second']
或$matches['message']
。