Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class StripHtmlFsm {
- private $state;
- private $empty = false;
- private $HTML;
- private $output = '';
- public function __construct($HTML) {
- $this->HTML = $HTML;
- $this->state = 'text';
- }
- public function parse_transform() {
- $STOPPER = 100;
- while (!$this->empty) {
- $this->{$this->state}();
- }
- }
- public function output() {
- return $this->output;
- }
- ## -----------------------------------------------
- ## parse/buffer tools
- ## -----------------------------------------------
- private function lookup($start_chars) {
- $len = strlen($start_chars);
- $extr = substr($this->HTML, 0, $len);
- echo "lookup '$extr'\n";
- if ($extr == $start_chars) {
- return true;
- }
- return false;
- }
- private function bufferize($str) {
- $this->output .= $str;
- }
- private function check_empty() {
- if (strlen($this->HTML) < 1) {
- echo "HTML is empty\n ";
- $this->empty = true;
- }
- }
- private function move($amount) {
- echo 'moving \'' . substr($this->HTML, 0, $amount) . "'\n";
- $this->bufferize(substr($this->HTML, 0, $amount));
- $this->HTML = substr($this->HTML, $amount);
- $this->check_empty();
- }
- private function delete($amount) {
- echo 'delete \'' . substr($this->HTML, 0, $amount) . "'\n";
- $this->HTML = substr($this->HTML, $amount);
- $this->check_empty();
- }
- ## -----------------------------------------------
- ## Etats de la FSM
- ## -----------------------------------------------
- private function change_state($state) {
- $transition = "{$this->state}_to_$state";
- echo "Transition $transition\n";
- $this->{$transition}();
- echo "New state $state\n";
- $this->state = $state;
- }
- private function text() {
- if ($this->lookup('<')) {
- $this->change_state('start_tag');
- }
- else {
- $this->move(1);
- /*echo "next\n";*/
- }
- }
- private function start_tag() {
- if ($this->lookup(' ')) {
- $this->change_state('attr_list');
- }
- elseif ($this->lookup('/>')) {
- $this->change_state('text');
- }
- elseif ($this->lookup('>')) {
- $this->change_state('text');
- }
- else {
- $this->delete(1);
- }
- }
- private function attr_list() {
- if ($this->lookup('="')) {
- $this->delete(2);
- $this->change_state('attr_val');
- }
- elseif ($this->lookup('/>')) {
- $this->change_state('text');
- }
- elseif ($this->lookup('>')) {
- $this->change_state('text');
- }
- else {
- $this->delete(1);
- }
- }
- private function attr_val() {
- if ($this->lookup('')) {
- }
- if ($this->lookup('\"')) {
- $this->delete(2);
- }
- elseif ($this->lookup('"')) {
- $this->delete(1);
- $this->change_state('attr_list');
- }
- else {
- $this->delete(1);
- }
- }
- ## -----------------------------------------------
- ## Transitions de la FSM
- ## -----------------------------------------------
- private function text_to_start_tag() {
- $this->delete(1);
- }
- private function start_tag_to_attr_list() {
- }
- private function attr_list_to_attr_val() {
- }
- private function attr_val_to_attr_list() {
- }
- private function attr_list_to_text() {
- $this->start_tag_to_text();
- }
- private function start_tag_to_text() {
- if ($this->lookup('/>')) {
- $this->delete(2);
- }
- else {
- $this->delete(1);
- $this->bufferize('_');
- }
- }
- }
- set_time_limit(3);
- header('content-type: text/plain; charset=utf8');
- ini_set('html_errors','off');
- $HTML = 'hallo mon nom est <strong class="qzd\">mou">Jeorg</strong>,<br/> et toi ?';
- $p = new StripHtmlFsm($HTML);
- $p->parse_transform();
- var_dump($p);
- echo $p->output();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement