3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * * PHP Version 5.5 * Classe de controle e contagem de palavras. * * @author Felipe Francisco <felipefrancisco@outlook.com> * @example Exemplo da estrutura JSON utilizada nessa classe: * * { * [0] => ( * ['palavras'] => ( * ['palavra_1'] => 5, * ['palavra_2'] => 8, * ['palavra_3'] => 1 * ), * ['total'] = '14' * ), * [1] => ( * ['palavras'] => ( * ['palavra_3'] => 3, * ['palavra_4'] => 6, * ['palavra_5'] => 10 * ), * ['total'] = '19' * ), * ['total_de_palavras'] => '33' * } */ namespace JSON; class Texto { /** * Vetor com todos os textos recebidos. * @var string[] */ private $textos = []; /** * Estrutura que será transformado em JSON. * @var string[] */ private $retorno = []; /** * Total de Palavras encontradas nos textos. * @var integer */ private static $totalPalavras; /** * Recebe a variável com o texto ou o conjunto de textos * e inicia o processo para contar e separar as palavras. * * @param string/string[] $textos (Texto ou vetor de textos) */ public function __construct($textos = null) { # Recebe automaticamente a variável caso ela não seja passada pelo argumento. if(!$textos) $textos = $_POST['textos']; # Verifica se o texto que está sendo fornecido é um texto válido. if(is_null($textos) || empty($textos)) throw new Exception("Por favor, insira um texto com conteúdo"); # Caso o argumento recebido não seja um conjunto de textos, transforme-o em um conjunto. if(!is_array($textos)) $this->textos[] = $texto; # Para cada texto do conjunto, adicioná-lo ao vetor de textos. foreach($textos as $t) $this->adicionaTexto($t); } /** * Adiciona um texto ao conjunto de textos retirando * as possíveis tags HTML que possam existir. * * @param string $texto (um texto qualquer) * @return void */ public function adicionaTexto($texto) { # Retira a pontuação do texto. $texto = preg_replace('/[\p{P}]/', "", $texto); # Retirando qualquer tag HTML por precaução. $this->textos[] = strtolower(strip_tags($texto)); } /** * Separa as palavras do texto e efetua a contagem delas * para adquirir um número total de textos e palavras. * * @param integer $i [description] * */ private function separaPalavras($i = 0) { $texto = $this->textos[$i]; # Caso não existam mais textos a serem tratados, encerrar a recursão. if(!$texto) return; # Separa as palavras no texto. $palavras = explode(' ', $texto); # Iteração entre as palavras encontradas. foreach($palavras as $p) { $this->retorno[$i]['palavras'][$p]++; $this->retorno[$i]['total']++; self::$totalPalavras++; } # Chama a função novamente para verificar se ainda existem textos. $this->separaPalavras(++$i); } /** * Gera o JSON das informações que estão na classe. * @return string (textos em JSON na estrutura pré-definida) */ public function gerarJSON() { # Reseta a contagem de palavras; self::$totalPalavras = 0; # Separa as palavras. $this->separaPalavras(); # Criando o JSON. $json = $this->retorno; $json['total_de_palavras'] = $this->totalPalavras; return json_encode($json); } } error_reporting(E_ALL ^ E_NOTICE); $textos = ['meu nome é joão', 'meu nome é Maria']; $oTexto = new Texto($textos); $oTexto->adicionaTexto('olá, eu sou felipe!'); $json = $oTexto->gerarJSON(); $decoded = json_decode($json); print_r($decoded); echo $json;
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XS7da
function name:  (null)
number of ops:  26
compiled vars:  !0 = $textos, !1 = $oTexto, !2 = $json, !3 = $decoded
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  149     0  E >   INIT_NS_FCALL_BY_NAME                                    'JSON%5Cerror_reporting'
          1        FETCH_CONSTANT                                   ~4      'JSON%5CE_ALL'
          2        FETCH_CONSTANT                                   ~5      'JSON%5CE_NOTICE'
          3        BW_XOR                                           ~6      ~4, ~5
          4        SEND_VAL_EX                                              ~6
          5        DO_FCALL                                      0          
  151     6        ASSIGN                                                   !0, <array>
  152     7        NEW                                              $9      'JSON%5CTexto'
          8        SEND_VAR_EX                                              !0
          9        DO_FCALL                                      0          
         10        ASSIGN                                                   !1, $9
  153    11        INIT_METHOD_CALL                                         !1, 'adicionaTexto'
         12        SEND_VAL_EX                                              'ol%C3%A1%2C+eu+sou+felipe%21'
         13        DO_FCALL                                      0          
  154    14        INIT_METHOD_CALL                                         !1, 'gerarJSON'
         15        DO_FCALL                                      0  $13     
         16        ASSIGN                                                   !2, $13
  155    17        INIT_NS_FCALL_BY_NAME                                    'JSON%5Cjson_decode'
         18        SEND_VAR_EX                                              !2
         19        DO_FCALL                                      0  $15     
         20        ASSIGN                                                   !3, $15
  157    21        INIT_NS_FCALL_BY_NAME                                    'JSON%5Cprint_r'
         22        SEND_VAR_EX                                              !3
         23        DO_FCALL                                      0          
  158    24        ECHO                                                     !2
         25      > RETURN                                                   1

Class JSON\Texto:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 6
Branch analysis from position: 3
2 jumps found. (Code = 47) Position 1 = 10, Position 2 = 12
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 17
Branch analysis from position: 13
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 25
Branch analysis from position: 22
2 jumps found. (Code = 77) Position 1 = 26, Position 2 = 31
Branch analysis from position: 26
2 jumps found. (Code = 78) Position 1 = 27, Position 2 = 31
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
Branch analysis from position: 25
Branch analysis from position: 12
Branch analysis from position: 6
filename:       /in/XS7da
function name:  __construct
number of ops:  33
compiled vars:  !0 = $textos, !1 = $texto, !2 = $t
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   61     0  E >   RECV_INIT                                        !0      null
   64     1        BOOL_NOT                                         ~3      !0
          2      > JMPZ                                                     ~3, ->6
   65     3    >   FETCH_R                      global              ~4      '_POST'
          4        FETCH_DIM_R                                      ~5      ~4, 'textos'
          5        ASSIGN                                                   !0, ~5
   68     6    >   INIT_NS_FCALL_BY_NAME                                    'JSON%5Cis_null'
          7        SEND_VAR_EX                                              !0
          8        DO_FCALL                                      0  $7      
          9      > JMPNZ_EX                                         ~8      $7, ->12
         10    >   ISSET_ISEMPTY_CV                                 ~9      !0
         11        BOOL                                             ~8      ~9
         12    > > JMPZ                                                     ~8, ->17
   69    13    >   NEW                                              $10     'JSON%5CException'
         14        SEND_VAL_EX                                              'Por+favor%2C+insira+um+texto+com+conte%C3%BAdo'
         15        DO_FCALL                                      0          
         16      > THROW                                         0          $10
   72    17    >   INIT_NS_FCALL_BY_NAME                                    'JSON%5Cis_array'
         18        SEND_VAR_EX                                              !0
         19        DO_FCALL                                      0  $12     
         20        BOOL_NOT                                         ~13     $12
         21      > JMPZ                                                     ~13, ->25
   73    22    >   FETCH_OBJ_W                                      $14     'textos'
         23        ASSIGN_DIM                                               $14
         24        OP_DATA                                                  !1
   76    25    > > FE_RESET_R                                       $16     !0, ->31
         26    > > FE_FETCH_R                                               $16, !2, ->31
   77    27    >   INIT_METHOD_CALL                                         'adicionaTexto'
         28        SEND_VAR_EX                                              !2
         29        DO_FCALL                                      0          
   76    30      > JMP                                                      ->26
         31    >   FE_FREE                                                  $16
   79    32      > RETURN                                                   null

End of function __construct

Function adicionatexto:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XS7da
function name:  adicionaTexto
number of ops:  17
compiled vars:  !0 = $texto
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   88     0  E >   RECV                                             !0      
   91     1        INIT_NS_FCALL_BY_NAME                                    'JSON%5Cpreg_replace'
          2        SEND_VAL_EX                                              '%2F%5B%5Cp%7BP%7D%5D%2F'
          3        SEND_VAL_EX                                              ''
          4        SEND_VAR_EX                                              !0
          5        DO_FCALL                                      0  $1      
          6        ASSIGN                                                   !0, $1
   94     7        INIT_NS_FCALL_BY_NAME                                    'JSON%5Cstrtolower'
          8        INIT_NS_FCALL_BY_NAME                                    'JSON%5Cstrip_tags'
          9        SEND_VAR_EX                                              !0
         10        DO_FCALL                                      0  $5      
         11        SEND_VAR_NO_REF_EX                                       $5
         12        DO_FCALL                                      0  $6      
         13        FETCH_OBJ_W                                      $3      'textos'
         14        ASSIGN_DIM                                               $3
         15        OP_DATA                                                  $6
   96    16      > RETURN                                                   null

End of function adicionatexto

Function separapalavras:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 7
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
2 jumps found. (Code = 77) Position 1 = 13, Position 2 = 25
Branch analysis from position: 13
2 jumps found. (Code = 78) Position 1 = 14, Position 2 = 25
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
filename:       /in/XS7da
function name:  separaPalavras
number of ops:  31
compiled vars:  !0 = $i, !1 = $texto, !2 = $palavras, !3 = $p
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  105     0  E >   RECV_INIT                                        !0      0
  107     1        FETCH_OBJ_R                                      ~4      'textos'
          2        FETCH_DIM_R                                      ~5      ~4, !0
          3        ASSIGN                                                   !1, ~5
  110     4        BOOL_NOT                                         ~7      !1
          5      > JMPZ                                                     ~7, ->7
  111     6    > > RETURN                                                   null
  114     7    >   INIT_NS_FCALL_BY_NAME                                    'JSON%5Cexplode'
          8        SEND_VAL_EX                                              '+'
          9        SEND_VAR_EX                                              !1
         10        DO_FCALL                                      0  $8      
         11        ASSIGN                                                   !2, $8
  117    12      > FE_RESET_R                                       $10     !2, ->25
         13    > > FE_FETCH_R                                               $10, !3, ->25
  119    14    >   FETCH_OBJ_RW                                     $11     'retorno'
         15        FETCH_DIM_RW                                     $12     $11, !0
         16        FETCH_DIM_RW                                     $13     $12, 'palavras'
         17        FETCH_DIM_RW                                     $14     $13, !3
         18        PRE_INC                                                  $14
  120    19        FETCH_OBJ_RW                                     $16     'retorno'
         20        FETCH_DIM_RW                                     $17     $16, !0
         21        FETCH_DIM_RW                                     $18     $17, 'total'
         22        PRE_INC                                                  $18
  122    23        PRE_INC_STATIC_PROP                                      'totalPalavras'
  117    24      > JMP                                                      ->13
         25    >   FE_FREE                                                  $10
  126    26        INIT_METHOD_CALL                                         'separaPalavras'
         27        PRE_INC                                          ~21     !0
         28        SEND_VAL                                                 ~21
         29        DO_FCALL                                      0          
  127    30      > RETURN                                                   null

End of function separapalavras

Function gerarjson:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XS7da
function name:  gerarJSON
number of ops:  14
compiled vars:  !0 = $json
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  136     0  E >   ASSIGN_STATIC_PROP                                       'totalPalavras'
          1        OP_DATA                                                  0
  139     2        INIT_METHOD_CALL                                         'separaPalavras'
          3        DO_FCALL                                      0          
  142     4        FETCH_OBJ_R                                      ~3      'retorno'
          5        ASSIGN                                                   !0, ~3
  143     6        FETCH_OBJ_R                                      ~6      'totalPalavras'
          7        ASSIGN_DIM                                               !0, 'total_de_palavras'
          8        OP_DATA                                                  ~6
  145     9        INIT_NS_FCALL_BY_NAME                                    'JSON%5Cjson_encode'
         10        SEND_VAR_EX                                              !0
         11        DO_FCALL                                      0  $7      
         12      > RETURN                                                   $7
  146    13*     > RETURN                                                   null

End of function gerarjson

End of class JSON\Texto.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
161.3 ms | 1408 KiB | 33 Q