WordPress.org

Ready to get started?Download WordPress

Forums

str_replace characters in the_content? special characters not matched? (7 posts)

  1. sepp88
    Member
    Posted 1 year ago #

    Hey guys,

    I really need your help with something I don't understand. The reason I don't understand the current behaviour of this function is that it sometimes "works" and in other cases doesn't.

    I want my blog to have the same quote-symbols for various quote marks.

    add_filter('the_content', 'wr_replace_text');
    
    function wr_replace_text($text){
        $replace = array(
                '“' => '»',
    	    'A' => 'B'
        );
        $text = str_replace(array_keys($replace), $replace, $text);
        return $text;
    }

    Imagine this text … 

    iam voluptua. “At vero” eos et a

    the output WordPress generates with the function above is this …

    iam voluptua. “Bt vero” eos et a

    So the "A" is replaced by "B" but the quotes aren't?

    It should actually be this

    iam voluptua. »Bt vero” eos et a

    But the "opening-quote" is not matched! Why isn't it replaced?

  2. s_ha_dum
    Member
    Posted 1 year ago #

    What does your curly quote look like in the source? Is it encoded?

  3. sepp88
    Member
    Posted 1 year ago #

    What do you mean by encoded?

    I see them as normal text in the source, not encoded. Some Replacements work, other don't. As if I had to specify a priority for the add_filter() function so that my replacements are always made.

  4. s_ha_dum
    Member
    Posted 1 year ago #

    What do you mean by encoded?

    The same as you are doing it right here: '“' => '»',

    echo $text; die; at the top of your function just to confirm. Your filter is not the only one in play. You may be right about needing to specify a priority.

  5. sepp88
    Member
    Posted 1 year ago #

    Well if I add echo $text; die; of course only the "title" of the post is printed because I'm adding the function to all possible contents like title, the_content, the_excerpt etc.

    add_filter('the_content', 'wr_replace_text', 100);
    add_filter('the_excerpt', 'wr_replace_text', 100);
    add_filter('the_title', 'wr_replace_text', 100);
    add_filter('category_description', 'wr_replace_text', 100);
    add_filter('term_description', 'wr_replace_text', 100);
    add_filter('pre_user_description', 'wr_replace_text', 100);

    If I remove the die all other texts get echoed as well so the filter is set correctly. It just doesn't work for the quotes! And I don't get why. It works if I set 'A' => 'B' but doesn't for the quote marks.

  6. sepp88
    Member
    Posted 1 year ago #

    It works for this elements perfectly …

    '...' => '…',
    '(c)' => '©',
    '‹' => '«',
    '›' => '»',
    '„' => '«',
    '“' => '»',

    … but doesn't for …

    `' - ' => ' — ',
    ' – ' => ' — ',
    ' –,' => ' —, ',

    And as mentioned above I don't know how to replace "upper-quotes" on both sides of a word.

    E.g. <blockquote>iam voluptua. “Bt vero” eos et a</blockquote>

    I have no replacement at the moment for those but would love to have
    <blockquote>iam voluptua. «Bt vero» eos et a</blockquote>

    And the dashes don't work either.

    <blockquote>dolores et - ea rebum</blockquote>

    I want every short "dash" (a normal dash) replaced by a "—" - but this doesn't take effect.

  7. s_ha_dum
    Member
    Posted 1 year ago #

    I just recreated your function locally and pretty much everything works just fine for me. The quotes replace, both sides, the dashes, everything. There is a potential problem with the dashed though. If someone types a double dash you will replace both of them and get double emdashes. You'd need to use preg_replace to avoid that.

    Something on your system is interfering with the replacement or you are running into some of the problems described here: http://www.dwheeler.com/essays/quotes-in-html.html

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.