$welcome = file_get_contents('/content/welcome'); $text = preg_replace_callback('/[\w-]{6,}/ui', function ($matches) { return mb_substr($matches[0], 0, 5)."#"; }, $welcome); echo $text;