Search code examples
phpregexwordpresscontact-formcontact-form-7

Contact Form 7 content explode by regular expressions


I have a contents of 'Contact Form 7' that I got from WP post. It looks something like this:

Your Name (required)
[text* your-name]

Your Email (required)
[email* your-email]

Subject
[text your-subject]

Your Message
[textarea your-message]

[submit "Send"]

I need to explode this content to an array by regular expressions. At the end of the process it should look like this:

$arr = array ( 
'text* your-name',
'email* your-email',
'text your-subject',
'textarea your-message',
'submit "Send"',
)

Does anyone have an idea how to do it by using regular expressions or any other way? Thanks :)


Solution

  • (?<=\[)([^\]]+)
    

    Try this.Grab the capture.See demo.

    http://regex101.com/r/yP3iB0/8

    $re = ""(?<=\\[)([^\\]]+)"";
    $str = "Your Name (required)\n[text* your-name]\n\nYour Email (required)\n[email* your-email]\n\nSubject\n[text your-subject]\n\nYour Message\n[textarea your-message]\n\n[submit \"Send\"]";
    
    preg_match_all($re, $str, $matches);