Insérer mots aléatoirement dans un texte auto généré
Posté : 25 avr. 2016, 10:31
Bonjour,
La semaine dernière, j'ai réussi à créer (grâce a la grande aide de "Spols") un formulaire qui, lorsqu'on le rempli avec des champs variables et que l'on "send", génère toutes les combinaisons possibles (cf : php-debutant/lire-des-tableaux-tableaux ... 75941.html).
Ce qui me donne comme script :
J'ai trouvé sur ce post : php-avance/fonction-rand-sur-tableau-t243541.html, qui, je pense me donne un partie de la solution!
Je l'ai donc adapter a mon cas ce qui me donne :
"zbif fgqdwrkz wjzzc enpqbxwzj frhgcps relrkyufns puz hq jkolffm oo Tu as Tu as chez le président le chien Tu as chez le président le chien Tu as fini chez le président Tu as Tu as"
Le petit plus...
Il faudrait que chaque combinaisons soit un lien...
Help me please..!
La semaine dernière, j'ai réussi à créer (grâce a la grande aide de "Spols") un formulaire qui, lorsqu'on le rempli avec des champs variables et que l'on "send", génère toutes les combinaisons possibles (cf : php-debutant/lire-des-tableaux-tableaux ... 75941.html).
Ce qui me donne comme script :
<?php
class spinning {
public $results = array();
public function process($text) {
preg_replace_callback(
'/\{(((?>[^\{\}]+)|(?R))*)\}/x', array($this, 'combine'), $text
);
}
public function combine($text) {
$this->results[] = explode('|', $text[1]);
}
}
if (!empty($_POST)) {
$spinning = new spinning();
$string = $_POST['ameliorer'];
$spinning->process($string);
$final_tab = $spinning->results;
$tab = array();
foreach ($final_tab as $all) {
$tab[] = sizeof($all);
}
for ($i = 0; $i < array_product($tab); $i++) {
$remplacement = array();
foreach ($tab as $cle => $taille) {
$index = array_product(array_slice($tab, $cle + 1));
$final_index = floor($i / $index) % $taille;
$remplacement[] = $final_tab[$cle][$final_index];
}
$phrase = $remplacement;
$phrase = implode($phrase, ' ');
echo $phrase . '<br>';
}
}
?>
<form method="post" action="seo.php">
<textarea name="ameliorer" id="ameliorer" rows="5" cols="100"></textarea>
<input type="submit" value="Envoyer">
</form>
Parfait mais pour la suite de l'exercice je dois maintenant faire en sorte d’intégrer ces combinaisons à l’intérieur d'un texte...J'ai trouvé sur ce post : php-avance/fonction-rand-sur-tableau-t243541.html, qui, je pense me donne un partie de la solution!
Je l'ai donc adapter a mon cas ce qui me donne :
<?php
class spinning {
public $results = array();
public function process($text) {
preg_replace_callback(
'/\{(((?>[^\{\}]+)|(?R))*)\}/x', array($this, 'combine'), $text
);
}
public function combine($text) {
$this->results[] = explode('|', $text[1]);
}
}
if (!empty($_POST)) {
$spinning = new spinning();
$string = $_POST['ameliorer'];
$spinning->process($string);
$final_tab = $spinning->results;
$tab = array();
foreach ($final_tab as $all) {
$tab[] = sizeof($all);
}
for ($i = 0; $i < array_product($tab); $i++) {
$remplacement = array();
foreach ($tab as $cle => $taille) {
$index = array_product(array_slice($tab, $cle + 1));
$final_index = floor($i / $index) % $taille;
$remplacement[] = $final_tab[$cle][$final_index];
}
$phrase = $remplacement;
$phrase = implode($phrase, ' ');
echo $phrase . '<br>';
}
------------------------------------------------------------------------------------------------------------------
$alphabet = range('a', 'z');
$language = null;
for ($e = 0, $words = rand(100, 500); $e < $words; $e++) {
for ($i = 0, $length = rand(2, 10); $i < $length; $i++)
$language .= $alphabet[array_rand($alphabet)];
$language .= ' ';
}
$tableau = $remplacement;
$pourcent = round($words * 0.05);
$dico = array();
for ($fix = 0; $fix < $pourcent; $fix++) {
$dico[] = $tableau[array_rand($tableau)];
}
$texteEntier_explode = explode(' ', $language);
$list = array_merge($texteEntier_explode, $dico);
$texteFinal = implode(' ', $list);
echo "$texteFinal";
----------------------------------------------------------------------------------------------------------------
}
?>
<form method="post" action="seo.php">
<textarea name="ameliorer" id="ameliorer" rows="5" cols="100"></textarea>
<input type="submit" value="Envoyer">
</form>
On se rapproche mais, le problème est qu'il me rajoute seulement des parties de combinaisons a la fin du texte qu'il génère... Aucune à l’intérieur et surtout aucune en entière!"zbif fgqdwrkz wjzzc enpqbxwzj frhgcps relrkyufns puz hq jkolffm oo Tu as Tu as chez le président le chien Tu as chez le président le chien Tu as fini chez le président Tu as Tu as"
Le petit plus...
Help me please..!