php - � appears using character_limiter() with strip_tags() and utf-8 charset -


i'm getting � characters when combine codeigniter's character_limiter() php's native strip_tags(). here code i'm using:

<?php echo character_limiter(strip_tags($block->body), 60); ?> 

$block->body html string stored in database. not unexpected output if use 1 of functions. looks this:

enter image description here

this html looks like:

enter image description here

i didn't paste actual html because string modified posting here, see below

here codeigniter function character_limiter:

function character_limiter($str, $n = 500, $end_char = '&#8230;') {     if (strlen($str) < $n)     {         return $str;     }      $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));      if (strlen($str) <= $n)     {         return $str;     }      $out = "";     foreach (explode(' ', trim($str)) $val)     {         $out .= $val.' ';          if (strlen($out) >= $n)         {             $out = trim($out);             return (strlen($out) == strlen($str)) ? $out : $out.$end_char;         }     } } 

i figured out there invisible character or may have been causing this, because when pasted html text editor, "html source editor" in image (which tinymce), saved it, weird characters disappeared.

i using utf-8 character set across board (everywhere possible). original data did come dump of unknown database, , imported sql client. however, when saved existing string (in cms), nothing changed.

i can't connect dots between these 2 functions causing output when used together, , do not � characters normally. only see output when use:

character_limiter(strip_tags($html)) 

what causing this, , how can prevent it?

note: want use character_limiter function, or variation of it. makes ellipsis @ end of string if length longer second param. using alone (without strip_tags) works fine (no weird characters).

update: can't reproduce this, put sql file online demos issue. importing mysql query browser. output seems when html comes database. here link (ignore content, it's client's fault): http://wesleymurch.com/test/test1.sql

� replacement character used replace unknown or unprintable character in php solve issue using multibyte string functions . use mb_substr strip tags :

mb_substr( strip_tags($text) , 0,300 ,'utf-8' );//or ever charset  

or maybe modify codeigniter function , use multibyte string functions .

update

function character_limiter($str, $n = 500, $end_char = '&#8230;') {     if (mb_strlen($str) < $n)     {         return $str;     }      $str = mb_ereg_replace("\s+", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));      if (mb_strlen($str) <= $n)     {         return $str;     }      $out = "";     foreach (explode(' ', trim($str)) $val)     {         $out .= $val.' ';          if (mb_strlen($out) >= $n)         {             $out = trim($out);             return (mb_strlen($out) == mb_strlen($str)) ? $out : $out.$end_char;         }     } } 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -