0

My hosting can't enable mbstring module, how to replace this piece of code? replacing mb_split ? This is not my code and i'm looking only to solve this.

My goals is using preg_replace or another func that's no mb_split

 /**
     * @param      $content
     * @param bool $amount
     *
     * @return mixed
     */
public function truncate($content, $amount = false)
{

    if (!$amount || preg_match_all("/\s+/", $content, $junk) <= $amount) return $content;

    $content = preg_replace_callback("/(<\/?[^>]+\s+[^>]*>)/", array($this, '_shield'), $content);

    $words   = 0;
    $output  = array();
    $content = str_replace(array("<", ">"), array(" <", "> "), $content);
    $tokens  = mb_split("\s+", $content);

    foreach ($tokens as $token) {
        // goes through tags and store them so they can get restored afterwards
        if (preg_match_all("/<(\/?[^\x01>]+)([^>]*)>/", $token, $tags, PREG_SET_ORDER)) {
            foreach ($tags as $tag) $this->_recordTag($tag[1], $tag[2]);
        }

        $output[] = trim($token);

        if (!preg_match("/^(<[^>]+>)+$/", $token)) {
            // if it's a real word outside tags, increase the count
            if (preg_match("/\p{L}+/u", $token)) $matching = true;
            else $matching = preg_match("/\w/", $token);

            if (!strpos($token, '=') && !strpos($token, '<') && strlen(trim(strip_tags($token))) > 0 && $matching) ++$words;
        }

        if ($words >= $amount) break;
    }

    $truncate = $this->_unshield(implode(' ', $output));

    return $truncate;
}
andrea
  • 381
  • 5
  • 23

0 Answers0