从PHP中的复杂字符串中提取值

时间:2018-08-25 11:06:46

标签: php

我试图从每一行中提取值字段中的内容。我被卡住了,因为我需要一个未知的开始和结束位置的子字符串。 例如,名称字段我需要值'wwwww',电子邮件字段我需要电子邮件值'aa@gmail.com'等。

由id爆炸,这是$ arr数组

(int) 0 => '{"name":{"',
    (int) 1 => '":"name","type":"text","title":"","value":"wwww","raw_value":"wwww","required":true},"email":{"',
    (int) 2 => '":"email","type":"email","title":"","value":"aa@gmail.com","raw_value":"aa@gmail.com","required":true},"phone":{"',
    (int) 3 => '":"phone","type":"text","title":"","value":"55555","raw_value":"55555","required":true},"message":{"',
    (int) 4 => '":"message","type":"textarea","title":"","value":"gjghjghj","raw_value":"gjghjghj","required":false}}'

  $name=null;; 
                $email=null; 
                $phone=null; 
                $message=null; 

                foreach( $arr as $key => $item2):

                    if (strpos($item2,'"name"') !== false) {

                       $arr2=explode(':',$arr[$key+1]);
                        debug($item2);
                        debug('name:'.$arr2[1]);
                      $name=$arr2[1];
                     exit;   
                   }
                   if (strpos($item2,'"email"') !== false) {

                       $arr2=explode(':',$arr[$key+1]);
                        // debug($arr2[0]);
                       //  debug('email:'.$arr2[1]);
                         $email=$arr2[1];
                   }
                    if (strpos($item2,'"phone"') !== false) {

                       $arr2=explode(':',$arr[$key+1]);
                        // debug($arr2[0]);
                       //  debug('phone:'.$arr2[1]);
                         $phone=$arr2[1];
                   }
                   if (strpos($item2,'"message"') !== false) {

                        $arr2=explode(':',$arr[$key+1]);
                        // debug($arr2[0]);
                      //    debug('message:'.$arr2[1]);
                        $message=$arr2[1];

                   }



                 endforeach;


                 $form[$i]['id']=  $item['meta_id'];  
                 $form[$i]['name']=  $name; 
                 $form[$i]['email']=  $email; 
                 $form[$i]['phone']=  $phone; 
                 $form[$i]['message']=  $message; 
                 $i=$i+1;

        // debug($arr);
           endforeach;  

1 个答案:

答案 0 :(得分:3)

似乎您的字符串是json字符串。

而不是使用expload
 $result = json_decode($jsondata, true);
 print_r($result);