我正在写一个regex
,我需要过滤的内容进行格式化的排版。到目前为止,我的代码似乎是正确使用preg_replace
过滤掉我的内容,但我无法弄清楚如何避免这种情况的某些标记内包裹的内容,说<pre>
。
作为参考,这是要的WordPress的the_content
过滤器中使用,所以我当前的代码看起来像这样:
function my_typography( $str ) {
$ignore_elements = array("code", "pre");
$rules = array(
"?" => array("before"=> " ", "after"=>""),
// the others are stripped out for simplicity
);
foreach($rules as $rule=>$params) {
// Pseudo :
// if( !in_array( $parent_tag, $ignore_elements) {
// /Pseudo
$formatted = $params['before'] . $rule . $params['after'];
$str = preg_replace( $rule, $formatted, $str );
// Pseudo :
// }
// /Pseudo
}
return $str;
}
add_filter( 'the_content', 'my_typography' );
基本上:
<p>Was this filtered? I hope so</p>
<pre>Was this filtered? I hope not.</pre>
应该成为
<p>Was this filtered ? I hope so</p>
<pre>Was this filtered? I hope not.</pre>
你需要用正则表达式搜索与正则表达式定界符preg_replace
而且必须调用preg_quote
逃避所有特殊的正则表达式字符,?
,.
,*
,+
等:
$str = preg_replace( '~' . preg_quote($rule, '~') . '~', $formatted, $str );
全码:
function my_typography( $str ) {
$ignore_elements = array("code", "pre");
$rules = array(
"?" => array("before"=> " ", "after"=>""),
// the others are stripped out for simplicity
);
foreach($rules as $rule=>$params) {
// Pseudo :
// if( !in_array( $parent_tag, $ignore_elements) {
// /Pseudo
$formatted = $params['before'] . $rule . $params['after'];
$str = preg_replace( '~' . preg_quote($rule, '~') . '~', $formatted, $str );
// Pseudo :
// }
// /Pseudo
}
return $str;
}
输出:
<p>Was this filtered ? I hope so</p>
<pre>Was this filtered ? I hope not.</pre>