I am getting data from the WordPress database. The data is like
Array ( [0] => stdClass Object ( [post_content] => [et_pb_section fb_built="1" admin_label="Hero" _builder_version="3.22" use_background_color_gradient="on" background_color_gradient_start="rgba(40,40,40,0.79)" background_color_gradient_end="rgba(40,40,40,0.86)" background_color_gradient_type="radial" background_color_gradient_direction_radial="top left" background_color_gradient_overlays_image="on" background_image="https://waydevelopers.tech/const/wp-content/uploads/2021/01/construction-35.jpg" custom_margin="||||false" custom_padding="10vw||10vw||true|false"][et_pb_row _builder_version="3.25"][et_pb_column type="4_4" _builder_version="3.25" custom_padding="|||" custom_padding__hover="|||"][et_pb_divider color="#ffb400" divider_position="center" divider_weight="5px" _builder_version="3.9" max_width="50px" module_alignment="center" locked="off"][/et_pb_divider][et_pb_text
module_id="homehead1"
_builder_version="4.8.0" text_font="||||||||" header_font="Rubik|700|||||||" header_font_size="64px" header_line_height="1.1em" text_orientation="center" background_layout="dark" max_width="800px" module_alignment="center" custom_margin="||0px|" custom_padding="|||16px" text_font_size_tablet="14px" text_font_size_last_edited="off|desktop" header_font_size_tablet="44px" header_font_size_phone="32px" header_font_size_last_edited="on|phone"] USA Construction [/et_pb_text][et_pb_text module_id="homesubhead1" _builder_version="4.8.0" text_font="||||||||" text_font_size="22px" text_line_height="1.6em" header_font="||||||||" text_orientation="center" background_layout="dark" max_width="800px" module_alignment="center" custom_margin="|||" custom_padding="20px||20px||true" text_font_size_tablet="18px" text_font_size_phone="" text_font_size_last_edited="on|phone"]Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam lectus erat consectetur eu sapien eget, rhoncus consectetur sem.[/et_pb_text][/et_pb_column][/et_pb_row][/et_pb_section][et_pb_section fb_built="1" admin_label=" Get a Quote" _builder_version="3.22" custom_padding="|||" animation_style="slide" animation_direction="top" animation_intensity_slide="10%" box_shadow_style="preset3" box_shadow_vertical="10px" box_shadow_blur="105px" box_shadow_spread="0px" box_shadow_color="rgba(0,0,0,0.12)"][et_pb_row column_structure="1_3,1_3,1_3" _builder_version="3.25" background_color="#ffffff" max_width="80%" custom_margin="|||" use_custom_width="on" width_unit="off"][et_pb_column type="1_3" _builder_version="3.25" custom_padding="|||" custom_padding__hover="|||"][et_pb_blurb title="Building Construction" image="https://waydevelopers.tech/const/wp-content/uploads/2021/01/construction-icon-7-gray.png" icon_placement="left" image_max_width="64px" content_max_width="1100px" content_max_width_tablet="1100px" content_max_width_last_edited="off|desktop" module_id="homeblurb1" _builder_version="4.8.0" header_font="Rubik|700|||||||" header_line_height="1.4em" body_font="||||||||" body_font_size="15px" body_line_height="1.8em" custom_margin="|||" custom_padding="|||" animation_style="slide" animation_direction="right" animation_intensity_slide="10%" animation="off" locked="off"]
I want to extract data within the square brackets by giving the module_id
as highlighted in the data above
And returning the data as et_pb_text module_id="homehead1" _builder_version="4.8.0" text_font="||||||||" header_font="Rubik|700|||||||" header_font_size="64px" header_line_height="1.1em" text_orientation="center" background_layout="dark" max_width="800px" module_alignment="center" custom_margin="||0px|" custom_padding="|||16px" text_font_size_tablet="14px" text_font_size_last_edited="off|desktop" header_font_size_tablet="44px" header_font_size_phone="32px" header_font_size_last_edited="on|phone"
and Also the Content between the respective blocks like
USA Construction
in between [et_pb_text...]Data to be extracted[/et_pb_text]
I tried preg match and other solutions but no luck at all I tried first converting the array to string and then applying preg match
$nn=json_encode($database);
preg_match_all("/\[[^\]]*\]/", $nn, $matches);
var_dump($matches);
If that string comes from the database, you can make the match more specific without matching the square brackets themselves and you don't have to use json_encode.
\[\Ket_pb_text module_id="homehead1"[^][]*(?=])
The pattern matches
\[\K
Match [
and reset the starting point of the reported match (clear the match buffer)et_pb_text module_id="homehead1"
Match literally to make the pattern more specific[^][]*
Match any char except [
and ]
(?=])
Assert ]
at the rightSee a regex demo | php demo.
$database = <<<DATABASE
Array ( [0] => stdClass Object ( [post_content] => [et_pb_section fb_built="1" admin_label="Hero" _builder_version="3.22" use_background_color_gradient="on" background_color_gradient_start="rgba(40,40,40,0.79)" background_color_gradient_end="rgba(40,40,40,0.86)" background_color_gradient_type="radial" background_color_gradient_direction_radial="top left" background_color_gradient_overlays_image="on" background_image="https://waydevelopers.tech/const/wp-content/uploads/2021/01/construction-35.jpg" custom_margin="||||false" custom_padding="10vw||10vw||true|false"][et_pb_row _builder_version="3.25"][et_pb_column type="4_4" _builder_version="3.25" custom_padding="|||" custom_padding__hover="|||"][et_pb_divider color="#ffb400" divider_position="center" divider_weight="5px" _builder_version="3.9" max_width="50px" module_alignment="center" locked="off"][/et_pb_divider][et_pb_text module_id="homehead1" _builder_version="4.8.0" text_font="||||||||" header_font="Rubik|700|||||||" header_font_size="64px" header_line_height="1.1em" text_orientation="center" background_layout="dark" max_width="800px" module_alignment="center" custom_margin="||0px|" custom_padding="|||16px" text_font_size_tablet="14px" text_font_size_last_edited="off|desktop" header_font_size_tablet="44px" header_font_size_phone="32px" header_font_size_last_edited="on|phone"] USA Construction [/et_pb_text][et_pb_text module_id="homesubhead1" _builder_version="4.8.0" text_font="||||||||" text_font_size="22px" text_line_height="1.6em" header_font="||||||||" text_orientation="center" background_layout="dark" max_width="800px" module_alignment="center" custom_margin="|||" custom_padding="20px||20px||true" text_font_size_tablet="18px" text_font_size_phone="" text_font_size_last_edited="on|phone"]Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam lectus erat consectetur eu sapien eget, rhoncus consectetur sem.[/et_pb_text][/et_pb_column][/et_pb_row][/et_pb_section][et_pb_section fb_built="1" admin_label=" Get a Quote" _builder_version="3.22" custom_padding="|||" animation_style="slide" animation_direction="top" animation_intensity_slide="10%" box_shadow_style="preset3" box_shadow_vertical="10px" box_shadow_blur="105px" box_shadow_spread="0px" box_shadow_color="rgba(0,0,0,0.12)"][et_pb_row column_structure="1_3,1_3,1_3" _builder_version="3.25" background_color="#ffffff" max_width="80%" custom_margin="|||" use_custom_width="on" width_unit="off"][et_pb_column type="1_3" _builder_version="3.25" custom_padding="|||" custom_padding__hover="|||"][et_pb_blurb title="Building Construction" image="https://waydevelopers.tech/const/wp-content/uploads/2021/01/construction-icon-7-gray.png" icon_placement="left" image_max_width="64px" content_max_width="1100px" content_max_width_tablet="1100px" content_max_width_last_edited="off|desktop" module_id="homeblurb1" _builder_version="4.8.0" header_font="Rubik|700|||||||" header_line_height="1.4em" body_font="||||||||" body_font_size="15px" body_line_height="1.8em" custom_margin="|||" custom_padding="|||" animation_style="slide" animation_direction="right" animation_intensity_slide="10%" animation="off" locked="off"]
DATABASE;
preg_match_all('/\[\Ket_pb_text module_id="homehead1"[^][]*(?=])/', $database, $matches);
var_dump($matches[0]);
Output
array(1) {
[0]=>
string(474) "et_pb_text module_id="homehead1" _builder_version="4.8.0" text_font="||||||||" header_font="Rubik|700|||||||" header_font_size="64px" header_line_height="1.1em" text_orientation="center" background_layout="dark" max_width="800px" module_alignment="center" custom_margin="||0px|" custom_padding="|||16px" text_font_size_tablet="14px" text_font_size_last_edited="off|desktop" header_font_size_tablet="44px" header_font_size_phone="32px" header_font_size_last_edited="on|phone""
}
Using json_encode, you would have to account for the backslashes
preg_match_all('/\[\Ket_pb_text module_id=\\\\"homehead1\\\\"[^][]*(?=])/', $nn, $matches);