0

I'm migrating from one Visual Page editor to another. I'm extracting the texts surrounded by the shortcode opening and closing tags.

Before I write my own parsing script, I wanted to make sure that Wordpress didn't already have a solution for this, or something else didn't already exist either. I feel like this is a common need that there may already be something to handle it.

Here is an example of the post_content data. As you can see this can get infinitely nested and complex.

I want to pull the data out and put it into an array. So that it's a lot easier to read, I highlighted the only two text phrases in the data.

[cs_content][cs_section parallax="false" separator_top_type="none" separator_top_height="50px" separator_top_angle_point="50" separator_bottom_type="none" separator_bottom_height="50px" separator_bottom_angle_point="50" style="margin: 0px;padding: 45px 0px;"][cs_row inner_container="true" marginless_columns="false" style="margin: 0px auto;padding: 0px;"][cs_column fade="false" fade_animation="in" fade_animation_offset="45px" fade_duration="750" type="1/1" style="padding: 0px;"][cs_text]this is text[/cs_text][/cs_column][/cs_row][/cs_section][cs_section parallax="false" separator_top_type="none" separator_top_height="50px" separator_top_angle_point="50" separator_bottom_type="none" separator_bottom_height="50px" separator_bottom_angle_point="50" style="margin: 0px;padding: 45px 0px;"][cs_row inner_container="true" marginless_columns="false" style="margin: 0px auto;padding: 0px;"][cs_column fade="false" fade_animation="in" fade_animation_offset="45px" fade_duration="750" type="1/1" style="padding: 0px;"][cs_text]text phrase 2[/cs_text][/cs_column][/cs_row][/cs_section][/cs_content]

Once it's extracted, I want to store it in an array like such:

$data = array(
    'this is text', 
    'text phrase 2'
);

I can handle that though. Just wanted to make sure I wasn't reinventing the wheel. Note, I don't care about any of the data within the shortcode definitions themselves.

2
  • Is the text you're after, always wrapped in [cs_text]...[/cs_text] ? – birgire Apr 28 '17 at 21:56
  • Ah, glad you caught that. No, it won't always exactly be [cs_text]...[/cs_text]. But it will always be [cs_]...[/cs_]. 'text' just means text element. There are 100s of elements cs_image, cs_slider, etc – user658182 Apr 29 '17 at 13:12
0

Not really a WordPress question, but you can use the PHP function preg_match_all to extract all the text wrapped in [cs_text] tags like so:

function wpse_265295_extract_cs_text( $subject ) {
  $pattern = "#\[cs_text\](.*?)\[/cs_text\]#s";
  preg_match_all( $pattern, $subject, $matches );
  return isset( $matches[1] ) ? $matches[1] : [];
}

If you're looking to extract arbitrary shortcodes:

if( ! function_exists( 'extract_shortcodes' ) ) :
function extract_shortcodes( $subject, $shortcode ) {
  $pattern = "#\[$shortcode\](.*?)\[/$shortcode\]#s";
  preg_match_all( $pattern, $subject, $matches );
  return isset( $matches[1] ) ? $matches[1] : [];
}
endif;
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.