skip to Main Content

I would like to rewrite quotes characters into the HTML element <q>.

❌ "Hello world", George said.

✅ <q>Hello world</q>, George said.

The text was written in the WordPress editor and will be displayed in the frontend via the_content(). In the editor, everything can stay as it is. The output of the quotes should only be changed in the frontend.

My approach is to use a filter to replace the quotes. However, I don’t know how to distinguish between opening and closing quotes.

function quote_filter( $content ) {
  $content = preg_replace( ??? );

  return $content;
}

add_filter( 'the_content', 'quote_filter' );

Do you have any ideas on how to solve this properly?

4

Answers


  1. I’ll use a for loop, alternating between the opening and closing tag when I reconstruct the string.

    <?php
    function quote_filter($content)
    {
      $arr = explode('"', $content);
      $brr = [];
      for ($i = 0; $i < count($arr); $i++) {
        $brr[] = $arr[$i];
        if ($i < count($arr) - 1) {
          $brr[] = $i % 2 == 0 ? '<q>' : '</q>';
        }
      }
    
      return implode("", $brr);
    }
    
    
    $content = 'hello "world" how "are" you?';
    echo quote_filter($content);
    

    BTW, AI would probably say:

    <?php
    function quote_filter($content)
    {
      $content = preg_replace_callback('/"([^"]*)"/', function ($matches) {
        return '<q>' . $matches[1] . '</q>';
      }, $content);
    
      return $content;
    }
    
    Login or Signup to reply.
  2. You could solve your direct problem as follows:

    $output = "";
    for ($index = 0; $index < strlen($input); $index++) {
        $open = true;
        switch ($input[$index]) {
            case $myChar: {
                $output .= ($open ? $prefix : $suffix);
                $open = !$open;
            } break;
            default: $output .= $input[$index];
        }
    }
    

    Of course you can make a function of it and vary what $myChar, $prefix and $suffix are, potentially adding styling, using other tags or other separators, as you wish.

    Login or Signup to reply.
  3. Try this

     function quote_filter( $content ) {
            $inQuote = false;
            $callback = function($matches) use (&$inQuote) {
                if ($inQuote) {
                    $inQuote = false;
                    return '</q>';
                } else {
                    $inQuote = true;
                    return '<q>';
                }
            };
                $content = preg_replace_callback('/"/', $callback, $content);
            return $content;
        }
        add_filter( 'the_content', 'quote_filter' );
    
    Login or Signup to reply.
  4. I have tried this code. It must work for you.

    <?php
    
    function quote_filter($content)
    {
        $is_open = false;
        $content = preg_replace_callback('/"([^"]*)"/', function ($matches) use (&$is_open) {
            if (!$is_open) {
                $is_open = true;
                return '<q>' . $matches[1] . '</q>';
            } else {
                $is_open = false;
                return '<q>' . $matches[1] . '</q>';
            }
        }, $content);
    
        return $content;
    }
    
    add_filter('the_content', 'quote_filter');
    
    
    

    Please let me know if any another query.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search