diff --git a/Parserus.php b/Parserus.php index 96a4b50..6ec64c2 100644 --- a/Parserus.php +++ b/Parserus.php @@ -1,7 +1,7 @@ * @link https://github.com/MioVisman/Parserus * @license https://opensource.org/licenses/MIT The MIT License (MIT) @@ -205,6 +205,7 @@ public function addBBCode(array $bb) } $res['handler'] = isset($bb['handler']) ? $bb['handler'] : null; + $res['text handler'] = isset($bb['text handler']) ? $bb['text handler'] : null; $required = []; $attrs = []; @@ -1067,6 +1068,49 @@ public function getCode($id = 0) return '[' . $tag . $def . $other . ']' . (isset($this->bbcodes[$tag]['single']) ? '' : $body . '[/' . $tag .']'); } + /** + * Метод возвращает текст без bb-кодов построенный на основании дерева тегов + * + * @param int $id Указатель на текущий тег + * + * @return string + */ + public function getText($id = 0) + { + if (isset($this->data[$id]['tag'])) { + + $body = ''; + foreach ($this->data[$id]['children'] as $cid) { + $child = $this->getText($cid); + if (isset($body{0}, $child{0})) { + $body .= ' ' . $child; + } else { + $body .= $child; + } + } + + $bb = $this->bbcodes[$this->data[$id]['tag']]; + + if (null === $bb['text handler']) { + return $body; + } + + $attrs = []; + foreach ($this->data[$id]['attrs'] as $key => $val) { + if (isset($bb['attrs'][$key])) { + $attrs[$key] = $val; + } + } + + return $bb['text handler']($body, $attrs, $this); + } + + $pid = $this->data[$id]['parent']; + $bb = $this->bbcodes[$this->data[$pid]['tag']]; + + return isset($bb['tags only']) ? '' : $this->data[$id]['text']; + } + /** * Метод ищет в текстовых узлах ссылки и создает на их месте узлы с bb-кодами url * Для уменьшения нагрузки использовать при сохранении, а не при выводе diff --git a/examples/getText.php b/examples/getText.php new file mode 100644 index 0000000..2fb2951 --- /dev/null +++ b/examples/getText.php @@ -0,0 +1,140 @@ +setBBCodes([ + ['tag' => 'table', + 'type' => 'table', + 'tags only' => true, + 'self nesting' => 3, + 'attrs' => [ + 'no attr' => true, + 'style' => true, + 'align' => true, + 'background' => true, + 'bgcolor' => true, + 'border' => true, + 'bordercolor' => true, + 'cellpadding' => true, + 'cellspacing' => true, + 'frame' => true, + 'rules' => true, + ], + 'handler' => function($body, $attrs) { + $attr = ''; + foreach ($attrs as $key => $val) { + $attr .= ' ' . $key . '="' . $val . '"'; + } + return '' . $body . ''; + }, + ], + ['tag' => 'tr', + 'type' => 'tr', + 'parents' => ['table', 't'], + 'tags only' => true, + 'self nesting' => 3, + 'attrs' => [ + 'no attr' => true, + 'style' => true, + ], + 'handler' => function($body, $attrs) { + $attr = ''; + foreach ($attrs as $key => $val) { + $attr .= ' ' . $key . '="' . $val . '"'; + } + return '' . $body . ''; + }, + ], + ['tag' => 'th', + 'type' => 'block', + 'parents' => ['tr'], + 'self nesting' => 3, + 'attrs' => [ + 'no attr' => true, + 'style' => true, + 'colspan' => true, + 'rowspan' => true, + ], + 'handler' => function($body, $attrs) { + $attr = ''; + foreach ($attrs as $key => $val) { + $attr .= ' ' . $key . '="' . $val . '"'; + } + return '' . $body . ''; + }, + ], + ['tag' => 'td', + 'type' => 'block', + 'parents' => ['tr'], + 'self nesting' => 3, + 'attrs' => [ + 'no attr' => true, + 'style' => true, + 'colspan' => true, + 'rowspan' => true, + ], + 'handler' => function($body, $attrs) { + $attr = ''; + foreach ($attrs as $key => $val) { + $attr .= ' ' . $key . '="' . $val . '"'; + } + return '' . $body . ''; + }, + ], + ['tag' => 'email', + 'type' => 'email', + 'attrs' => [ + 'Def' => [ + 'format' => '%^[^\x00-\x1f\s]+?@[^\x00-\x1f\s]+$%', + ], + 'no attr' => [ + 'body format' => '%^[^\x00-\x1f\s]+?@[^\x00-\x1f\s]+$%D', + 'text only' => true, + ], + ], + 'handler' => function($body, $attrs) { + if (empty($attrs['Def'])) { + return '' . $body . ''; + } else { + return '' . $body . ''; + } + }, + 'text handler' => function($body, $attrs) { + if (empty($attrs['Def'])) { + return $body; + } else { + return $body . ' ' . $attrs['Def']; + } + }, + ], +])->parse(' +[table align=right border=1 bordercolor=#ccc cellpadding=5 cellspacing=0 style="border-collapse:collapse; width:500px"] + [tr] + [th style="width:50%"]Position[/th] + [th style=width:50%]Astronaut[/th] + [/tr] + [tr] + [td]Commander[/td] + [td]Neil A. Armstrong[/td] + [/tr] + [tr] + [td]Command Module Pilot[/td] + [td]Michael Collins[/td] + [/tr] + [tr] + [td]Lunar Module Pilot[/td] + [td]Edwin "Buzz" E. Aldrin, Jr.[/td] + [/tr] +[/table] +[email=spam@mail.ru]My email[/email] +[email]superspam@mail.ru[/email] +')->getText(); + +#output: +# +#Position Astronaut Commander Neil A. Armstrong Command Module Pilot Michael Collins Lunar Module Pilot Edwin "Buzz" E. Aldrin, Jr. +#My email spam@mail.ru +#superspam@mail.ru +#