i want to save data i created randomly in a txt file ,but only keep the non duplicate data and also only display on a broswer the non duplicate data how can i do it in PHP ?
i try to create different sentences with a content spinning code; but i encounter 2 problems the first one with my code i can't display non duplicate sentences , and when i want to save them into a txt file ,he ony take 1 sentence and do not write the others .
here is my code:
<?php
function RandomContent($txt)
{
$pattern = '#\{([^{}]*)\}#msi';
$test = preg_match_all($pattern, $txt, $out);
$toFind = array();
$toReplace = array();
foreach ($out[0] as $id => $match) {
$choices = explode("~", $out[1][$id]);
$toFind[] = $match;
$toReplace[] = trim($choices[rand(0, count($choices) - 1)]);
}
return str_replace($toFind, $toReplace, $txt);
}
$role1= "{hero~king~president}";
$role2="{warrior~soldier~pilote}";
$text = "\n" . "i'm not a ". $role1 . " but i'm a " . $role2;
$numberOfSentences= 10;
for ($i = 0; $i <= $numberOfSentences - 1; $i++) {
echo "<pre>";
echo RandomContent($text);
$texttoregistry = RandomContent($text);
file_put_contents('Random4.txt', $texttoregistry, FILE_APPEND);
echo " || your data is registry into the ramdon file txt";
}
$lines = file('Random4.txt');
$lines = array_unique($lines);
i had try several thing to try in a first time save all line i create with the ramdon function by using FILE_APPEND
and to delete them from the Random4.txt i use array_unique but this didn't work .
here is the result in my Random4.txt and also on a browser :
i'm not a hero but i'm a soldier
i'm not a hero but i'm a pilote
i'm not a hero but i'm a warrior
i'm not a hero but i'm a pilote
i'm not a king but i'm a warrior
i'm not a president but i'm a pilote
i'm not a king but i'm a soldier
i'm not a king but i'm a soldier
i'm not a king but i'm a pilote
what i expect is something like this :
i'm not a hero but i'm a soldier
i'm not a hero but i'm a warrior
i'm not a hero but i'm a pilote
i'm not a king but i'm a soldier
i'm not a king but i'm a warrior
i'm not a king but i'm a pilote
i'm not a president but i'm a soldier
i'm not a president but i'm a warrrior
i'm not a president but i'm a pilote
Can't someone Lead me to find a solution please ? thank you by advance
Aucun commentaire:
Enregistrer un commentaire