2
I’m trying to solve a problem with initializing a variable, how to fix it?
<?php
$texto1 = $_POST['texto1'];
$texto2 = $_POST['texto2'];
$texto3 = $_POST['texto3'];
function randString($size) // função que gera string aleatória de caracteres
{
$basic='abcdefghijklmnopqrstuvwxyz';
$return = "";
for($count= 0; $size > $count; $count++){
$return .= $basic[rand(0, strlen($basic) - 1)];
}
return $return;
}
$stringbase = randString(1024); //colocando a string aleatória de 1024 caracteres na variável
function buscaPalavra($texto1,$texto2,$texto3) // função para fazer a busca de uma palavra em um texto
{
global $stringbase;
$contador1 = 0;
$contador2 = 0;
$contador3 = 0;
$palavra1 = "";
//$palavra2 = "";
//S$palavra3 = "";
//i=0
//j=0
//stringbase[i]=b
//texto[j]=d
for ($i = 0 , $j = 0 ; $i < strlen($stringbase) ; $i++)
{
if ($texto1[$j] == $stringbase[$i])
{
$palavra1 = $palavra1. "" . $stringbase[$i];
$j++; //eu quero que ele incremente
}
}
echo $palavra1."<br />";
echo $texto1 . ' apareceu ' . $contador1 . ' vezes, ' . $texto2 . ' apareceu ' . $contador2 . ' vezes e ' . $texto3 . 'apareceu ' . $contador3 . ' vezes ';
}
echo buscaPalavra($texto1,$texto2,$texto3);
?>
The mistake I see is this
Notice: Uninitialized string offset: 2 in C: xampp htdocs files index.php on line 48 The problem is in the $j variable
Here the separate html file for easy understanding
<!DOCTYPE html>
<html lang="pt_BR">
<head>
<title> Formulário de teste </title>
<meta charset="utf8" />
<link rel="stylesheet" type="text/css" href="css.css" />
</head>
<body>
<form action="http://localhost/arquivos/index.php" method="POST" align="center">
<h1 align="center"> Formulário de Teste </h1>
Texto1:
<input name="texto1" type="text" maxlength="8" onchange="this.value=this.value.toLowerCase()"/><br /><br />
Texto2:
<input name="texto2" type="text" maxlength="8" /><br /><br />
Texto3:
<input name="texto3" type="text" maxlength="8" /><br /><br />
<input type="Submit" name="Enviar" value="Enviar" />
</form>
</body>
</html>
What is line 48?
– KaduAmaral
if ($texto1[$j] == $stringbase[$i]) //linha 48
– Carlos Renato
What are the texts you are testing?
– KaduAmaral
Here’s what I’m doing in parts first just by putting text 1 of the first text field, then I do with the rest text2 and text3 I can’t use strpos, I have to look for a word in a 1024 character random string, in case it’s 3 words or 3 fields , I have to create a search algorithm
– Carlos Renato
for example in the text I am only using the word "do" inside this random string it has to count how many occurrences of that word is repeated in the string
– Carlos Renato