Search code examples
phpwordpressword-wrapadd-filter

wordwrap issues with special characters in wordpress


In my WordPress blog, I inserted the following content in "Description" field in add new post form.

sss'ss''ss'''ss"ss""sss"""

I am applying the filter hook(add_filter) the the_content() function as below. Basically I want to display non spaced words if it exist more than 50 character means, I would like to give space after 50 characters. So I used wordwrap function to do that. But while use special characters in content I am getting wired output as mentioned below.

function.php

function filter_content($content) {
  global $post;  
     $original = wordwrap($content,50,' ',' ' ); 
  return $original;
}
add_filter( 'the_content', 'filter_content' );

Output :

   sss’ss”ss”’ss”ss& #8221;"sss”"”

Solution

  • Issue:

    wordwrap function is converting special charters(quotes, double quotes and etc..) into html entity and count as a string.50 character occurrence happen between & and # from . While it divide that we got output as & #8221;.

    Solution:

    Instead of php just try css solution word-wrap property. So apply word-wrap:break-word; style to your paragraph tag. break-word value will force to break the word. But there is no option to give numbers like 50 and all. It will wrap depends on container width.

    p{
     word-wrap:break-word; 
    }