skip to Main Content

I have a multi-line string that I’m trying to extract, but it is not returning any pieces.

$input = '<script>
buffy = 1;
vampire = {
response: [{"sleep":"night"}]
};
</script>';

preg_match( '~response:(.*?)};~', $input, $output );

print_r( $output );

I am trying to extract [{"sleep":"night"}]

2

Answers


  1. check this out :

    $input = '<script>
    buffy = 1;
    vampire = {
    response: [{"sleep":"night"}]
    };
    </script>';
    
    preg_match('/response:s*([^;]+);/', $input, $output);
    
    print_r($output[1]);
    
    Login or Signup to reply.
  2. '~response: ([{[^}]+}])~'
    
    response    interpret as text
    :           interpret as text
                interpret as text (one space)
    (           start of capturing group
    [          interpret as text
    {          interpret as text
    [^}]        anything up to the next }
    +           ... and do this one or more times
    }           interpret as text
    ]           interpret as text
    )           end of capturing group
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search