1

I'm writing a regex where I need to filter content to format it's typography. So far, my code seems to be filtering out my content properly using preg_replace, but I can't figure out how to avoid this for content wrapped within certain tags, say <pre>.

As a reference, this is to be used within WordPress's the_content filter, so my current code looks like so:

function my_typography( $str ) {
    $ignore_elements = array("code", "pre");

    $rules = array(
        "?" => array("before"=> "&thinsp;", "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' );

Basically:

<p>Was this filtered? I hope so</p>
<pre>Was this filtered? I hope not.</pre> 

should become

<p>Was this filtered&thinsp;? I hope so</p>
<pre>Was this filtered? I hope not.</pre>

1 Answer 1

1

You need to wrap search regex with regex delimiter in preg_replace and must call preg_quote to escape all special regex characters such ?, ., *, + etc:

$str = preg_replace( '~' . preg_quote($rule, '~') . '~', $formatted, $str );

Full Code:

function my_typography( $str ) {
    $ignore_elements = array("code", "pre");

    $rules = array(
        "?" => array("before"=> "&thinsp;", "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;
}

Output:

<p>Was this filtered&thinsp;? I hope so</p>
<pre>Was this filtered&thinsp;? I hope not.</pre>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the feedback. This does in fact manage the rules properly; however, my HTML is "broken" in the sense that in the expected output, </p> is returned as < / p > which breaks my page. Is there something I am missing?
Your sample data doesn't have </a> anywhere but this code is just replacing ? with something. Can you update the question that recreates this problem?
turns out my breaking HTML was one of the rules, had a rule for "/" -- sorry about that! here'S the checkmark, thanks for your time !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.