【问题标题】:Extracting specific texts between square brackets in PHP在 PHP 中提取方括号之间的特定文本
【发布时间】: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


【解决方案1】:

如果该字符串来自数据库,您可以在不匹配方括号本身的情况下使匹配更加具体,并且您不必使用 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);

【讨论】:

  • 你可以使用相同的模式,将 homehead1 更改为 text1 regex101.com/r/8C82Hn/1
  • 你能优化这个正则表达式吗?我尝试使用您答案中的要点。 regex101.com/r/jlLI6F/2
  • 我希望在这个正则表达式regex101.com/r/jlLI6F/2中得到与现在相同的结果
  • 如果您希望点匹配换行符,您可以使用 s 标志或内联修饰符 (?s)regex101.com/r/uuHbeC/1 使用否定字符类不匹配 [] 不会在这种情况下工作,因为这部分 edited="on|phone"] 有一个右方括号。
  • @HasaanCh 你可以匹配 module_id="text2" 而不是像这样(?s)\[et_pb_text [^][]*module_id="text2"[^][]*].*?\[\/et_pb_text] regex101.com/r/8ybnP8/1 的方括号
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-24
  • 2010-09-27
  • 2012-04-23
  • 1970-01-01
  • 1970-01-01
  • 2014-08-07
  • 2011-01-25
相关资源
最近更新 更多