在 PHP 中提取方括号之间的特定文本
Extracting specific texts between square brackets in PHP
我正在从 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="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"]
我想通过给出上面数据中突出显示的 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"
以及各个块之间的内容
喜欢
USA Construction
在[et_pb_text...]之间要提取的数据[/et_pb_text]
我试过预匹配和其他解决方案,但一点运气都没有
我尝试先将数组转换为字符串,然后应用 preg match
$nn=json_encode($database);
preg_match_all("/\[[^\]]*\]/", $nn, $matches);
var_dump($matches);
如果该字符串来自数据库,您可以使匹配更具体而不匹配方括号本身,并且您不必使用 json_encode.
\[\Ket_pb_text module_id="homehead1"[^][]*(?=])
模式匹配
\[\K
匹配[
并重置报告匹配的起点(清除匹配缓冲区)
et_pb_text module_id="homehead1"
字面匹配使模式更具体
[^][]*
匹配除 [
和 ]
之外的任何字符
(?=])
在右边断言]
看到一个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]);
输出
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""
}
使用 json_encode,您必须考虑反斜杠
preg_match_all('/\[\Ket_pb_text module_id=\\"homehead1\\"[^][]*(?=])/', $nn, $matches);
我正在从 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="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"]
我想通过给出上面数据中突出显示的 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"
以及各个块之间的内容 喜欢
USA Construction
在[et_pb_text...]之间要提取的数据[/et_pb_text]
我试过预匹配和其他解决方案,但一点运气都没有 我尝试先将数组转换为字符串,然后应用 preg match
$nn=json_encode($database);
preg_match_all("/\[[^\]]*\]/", $nn, $matches);
var_dump($matches);
如果该字符串来自数据库,您可以使匹配更具体而不匹配方括号本身,并且您不必使用 json_encode.
\[\Ket_pb_text module_id="homehead1"[^][]*(?=])
模式匹配
\[\K
匹配[
并重置报告匹配的起点(清除匹配缓冲区)et_pb_text module_id="homehead1"
字面匹配使模式更具体[^][]*
匹配除[
和]
之外的任何字符
(?=])
在右边断言]
看到一个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]);
输出
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""
}
使用 json_encode,您必须考虑反斜杠
preg_match_all('/\[\Ket_pb_text module_id=\\"homehead1\\"[^][]*(?=])/', $nn, $matches);