' . '([\s\S,\n]*?)' . '#'; $matches = array(); if(preg_match_all($pattern, $string, $matches)) { $matchesCount = count($matches[0]); for($i = 0; $i < $matchesCount; $i++) { $section = trim($matches[1][$i]); $place = trim($matches[2][$i]); $value = trim($matches[3][$i]); $blockContent->add($section, $place, $value); } } return $blockContent; } /** * Convert block content to string. * * @param BlockContent $content Block content. * @return string */ public static function toString(BlockContent $content) { $result = ''; foreach ($content->getList() as $item) { $result .= '\n" . $item['value'] . "\n" . '\n"; } return trim($result); } /** * Sanitize. * * @param string $string String. * @return string */ public static function sanitize($string) { if (!self::isValid($string)) { return Block\Sanitizer::clean($string); } $content = self::toArray($string); $list = $content->getList(); foreach ($list as $index => $item) { if ($item['type'] !== BlockContent::TYPE_BLOCKS) { continue; } $item['value'] = Block\Sanitizer::clean($item['value']); $list[$index] = $item; } $content->setList($list); return self::toString($content); } }