Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I made a custom parse function for my plugin and this one works great. I just have a little problem !

Here is my function :

public function ys_glossary_parse( $text )
    {
        // Get all the items in the database
        $terms = $this->get_all_termes();

        // Get the id of the glossary page
        $glossary_page_id = get_option('ys_glossary_PageID');
        if(!$glossary_page_id OR $glossary_page_id == 0)
        {
            return $this->admin_message('You don\'t have a Glossary page yet. Go in the option page !');
        }

        // Get the permalink of the page
        $permalink = get_permalink($glossary_page_id);

        $text_to_link    = apply_filters( 'ys_glossary_text', $terms );

        //$preg_flags      = $case_sensitive ? 's' : 'si'; // TODO: add an option for that
        $preg_flags      = 'si';

        $text = ' ' . $text . ' ';
        if ( ! empty( $text_to_link ) ) {
            foreach ( $text_to_link as $old_text ) {

                // TODO: modify this to use the upper/lower case of each words without modifying them

                $new_text = '<a href="' . esc_url( $permalink ) . '#'.$old_text.'" title="Lien vers la definition" target="_blank" >' . $old_text . '</a>';
                $new_text = apply_filters( 'ys_glossary_text_linked_text', $new_text, $old_text, $permalink );
                $text = preg_replace( "|(?!<.*?)\b$old_text\b(?![^<>]*?>)|$preg_flags", $new_text, $text );
            }
            // Remove links within links
            $text = preg_replace( "#(<a [^>]+>)(.*)<a [^>]+>([^<]*)</a>([^>]*)</a>#iU", "$1$2$3$4</a>" , $text );
        }
        return trim( $text );
    }

The $terms are in a simple array like :

array(
[0] => 'item1', 
[1] => 'item2', 
[2] => 'item3'
); 

(they are with uppercase on the first letter).

What i want is :

How do i make the link on the words without changing the words case ? Blocking will stay as Blocking but blocking will be blocking. (So the uppercase and lowercase of the words stay as they are !)

I use the case insensitive but my function change all the words with a uppercase for the first letter because the terms are like that. But if i use lowercase on the terms, it change all to lowercase the_content.

So i need help on this.

Thank you very much ^^

Yann

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Kitefr, (this isn't really a WordPress question, but a php one) but you could usepreg_replace_callback, in which you define your own function to handle the the replacement.

For instance,

    $text = "The quick brown fox jumped over the Quick dog.";
    $text = preg_replace_callback( "/quick/si", 'my_replace' , $text );
    function my_replace($matches){
         return '<strong>'.$matches[0].'</strong>';
    }

This replaces all instances of 'quick' (in any case) and wraps them with strong tags, leaving the case of the letters unchanged. You could do something similar with link tags

share|improve this answer
    
Thank you, works like a charm ! ^^ –  Kitefr Jan 22 '12 at 22:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.