Contact Form 7 content explode by regular expressions

眉间皱痕 提交于 2019-12-12 17:38:16

问题


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 :)


回答1:


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

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);



回答2:


Note: Sorry for Answering to this thread generated a long ago, but this will help other surely.

Most of developer while developing a task related to the CF7, they need the type and name of the fields, but generally they failed to grab as the form is stored as a content due to custom post type.

So to fetch type, name, basetype, etc... you need to fetch all contact form post. You need to loop through every contact form post and pass the content of the form in below scan function as a agument

    foreach($form as $objForm){
     $manager = WPCF7_FormTagsManager::get_instance();
     $tags  = $manager->scan( $objForm->form );
     $filter_result = $manager->filter( $tags, $cond );
      foreach ($filter_result as $key => $value) {
          echo $value->type;
          echo $value->name;
      }
   }

Hope this help others :)



来源:https://stackoverflow.com/questions/27090522/contact-form-7-content-explode-by-regular-expressions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!