【发布时间】:2021-05-25 15:50:48
【问题描述】:
我正在从 WordPress 数据库中获取数据。数据是这样的
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="左上" 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"] 美国建筑 [/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="获取报价" _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="建筑施工" 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"]
我想通过给出上面数据中突出显示的module_id 来提取方括号内的数据
并将数据返回为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"
以及各个块之间的内容 喜欢
美国建筑
在[et_pb_text...]之间要提取的数据[/et_pb_text]
我尝试了 preg match 和其他解决方案,但完全没有运气 我尝试先将数组转换为字符串,然后应用预匹配
$nn=json_encode($database);
preg_match_all("/\[[^\]]*\]/", $nn, $matches);
var_dump($matches);
【问题讨论】:
-
你可以匹配
\[\Ket_pb_text module_id="homehead1"[^][]*(?=])regex101.com/r/7lTuo3/1 -
答案成功了吗?
-
是的,它完美无缺。
标签: php arrays wordpress string preg-match