Я пишу 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' );
В основном:
<code><p>Was this filtered? I hope so</p>
<pre>Was this filtered? I hope not.
должно стать
<code><p>Was this filtered ? I hope so</p>
<pre>Was this filtered? I hope not.