理解preg_replace的困难

时间:2012-03-18 15:01:48

标签: php preg-replace

目前我正在使用一个小搜索并替换我的一个WordPress网站的片段:

if ( ! function_exists( 'cor_remove_personal_options' ) ) {
  function cor_remove_personal_options( $subject ) {
    $pattern = '#<h3>Personal Options</h3>.+?/table>#s';
    $subject = preg_replace( $pattern, '', $subject, 1 );
    return $subject;
  }

  function cor_profile_subject_start() {
    ob_start( 'cor_remove_personal_options' );
  }

  function cor_profile_subject_end() {
    ob_end_flush();
  }
}
add_action( 'admin_head-profile.php', 'cor_profile_subject_start' );
add_action( 'admin_footer-profile.php', 'cor_profile_subject_end' );

preg_replace相对较新我想知道开始和结束数字符号# #s的确切功能。

我想知道是否有人可以向我解释这个?

2 个答案:

答案 0 :(得分:1)

这些唱歌决定了替换模式的开始和结束。 “s”是修饰符http://php.net/manual/en/reference.pcre.pattern.modifiers.php

答案 1 :(得分:0)

您可以使用/作为开始和结束字符或#。如果你使用#,那么斜杠不会被视为一个特殊字符,所以这也是有效的转义斜杠字符:

'/<h3>Personal Options<\/h3>.+?\/table>/s'

/ s或#s表示修饰符,告诉PHP正则表达式中的任何点都将被评估为任何字符,包括新行。