0

I am trying to extract from each row what is in the value field. I am getting stuck as i need a substring with unknown start and end position. eg the name field i need the value 'wwwww', the email field i need the email value 'aa@gmail.com' etc .

exploded by id and this is the $arr array

(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;  
ajt2
  • 37
  • 1
  • 7
  • 5
    It may be that your original string is JSON and you would be better of decoding it with `json_decode()` rather than splitting it by id. – Nigel Ren Aug 25 '18 at 11:12
  • Welcome to Stack Overflow! Please clean up your code example in the question: I am confused what the input is. Also, commented-out debug code doesn't belong here. – Gogowitsch Aug 25 '18 at 11:14
  • yes i agree to include the input but i couldnt include the input as it would let me for some reason? the input is just the string concatenated you see – ajt2 Aug 25 '18 at 11:23
  • @gogowitsch Cant you do that? And help the OP and other readers? – Lawrence Cherone Aug 25 '18 at 11:27
  • ok i wasnt aware of json encoded string so it wasnt intentional – ajt2 Aug 25 '18 at 12:21

1 Answers1

4

It seems your string is a json string. Instead of using expload use

 $result = json_decode($jsondata, true);
 print_r($result);
Anshu Kumar
  • 807
  • 1
  • 8
  • 27