提取双引号中的字符串(string处有一对双引号)


extract string in double quotation(there are pair double quotations at string)

我有一个字符串

$sting=
'   [
        type="user"
        name="ali"
        key="#$WRE"
        //problem
        address="{
            "type":"hellow"
        }"
    ]';

和I以key=value格式提取数据

for (;;) {
if(!preg_match('~([A-Za-z0-9]+)'='"([^'"]*)'"~m', $string,$match)){
    break;
}
$string=str_replace($match[0], '', $string);
$dataArray[$match[1]]=$match[2];
}
echo "<br><pre>";
print_r($dataArray);
echo "<br></pre>";

但输出是

<br><pre>Array
(
    [type] = user
    [name] = ali
    [key] = #$WRE
    [address] = {
				
)
<br></pre>
根据[地址](我英语不太好,因此句子中可能有错误)

请帮帮我

你可以使用像

这样的正则表达式
'/('w+)'s*='s*"(.*?)"'s*(?=$|'w+'s*=)/ms'

查看regex演示

<<p> 模式细节/strong>:
  • /s是一个DOTALL修饰符,它使.匹配任何符号,包括换行符
  • /m修饰符使$匹配行尾
  • ('w+) -组1捕获1个或多个字母数字或下划线字符
  • 's* -零或多个空白
  • = -一个等号
  • 's* - 0+ whitespaces
  • "(.*?)" -双引号,除换行符外,零或更多字符尽可能少,直到第一个双引号和这个引号(组2是在双引号之间捕获的)
  • 's* -零或多个空白
  • (?=$|'w+'s*=) -一个正向前看,要求字符串的末尾出现在当前位置或一个或多个字母数字后面,后跟0个或多个空格和一个等号。