How can I extract or parse data from post contents’ shortcodes into an array?

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.

Read more here: How can I extract or parse data from post contents’ shortcodes into an array?

Leave a Reply

Your email address will not be published. Required fields are marked *