0
In PHP I made a script that takes each word of a Tring and turns them into an array, giving explodes into empty spaces. Then he eliminates the repeated words and shows each of them, like this:
<?php
$hashtag = "#caio #azul #caio #azul #leão #orelha #caio #caio #caio";
//
$hashtagArray = explode(" ", $hashtag);
// exclui elementos vazios
$hashtagArray = array_filter($hashtagArray);
// exclui elementos iguais
$hashtagArray = array_unique($hashtagArray);
// reseta as chaves
$hashtagArray = array_values($hashtagArray);
// cria uma array
$hashtagArrayDuplica = array();
foreach ($hashtagArray as &$value) {
// repito a checagem, para evitar erros
if (!in_array($value, $hashtagArrayDuplica)) {
$hashtagArrayDuplica[] = $value;
echo $value."<br>";
}
}
?>
The result obtained is this:
#caio
#azul
#leão
#orelha
Which seems to be right. But incredibly on the client server, this application keeps showing the repeated items, practically disregarding my code, I don’t know if it’s for the PHP version, or any other reason (I can’t see the script running there, I only get print screen)... what can it be? Can there be any special character? Is there another way to do this check, to see if the server works?
PS: Notice that in my code I try to check 2 times if you have something repeated, it was an attempt not to give error in the client, but without success.
Ask the client to print the
phpinfo()
– Costamilam
Only
explode
,array_filter
andarray_unique
who has in question do what you want. See here. The problem is certainly not of the version as these 3 functions are supported from PHP4 (you can confirm in the documentation of each). I suspect the problem is another.– Isac
I checked and there is running version 5.6
– caiocafardo
Maybe some way to clear every word by taking out any different character?
– caiocafardo
The problem shouldn’t be in that code, it’s something else. I suggest starting by activating all the warnings to try to see any other problems that might exist. And you can test each function individually on a new test page step by step until you realize where the problem is.
– Isac
You can test your code in different versions of PHP at this link http://sandbox.onlinephpfunctions.com/code/04bd3a78eabf54735755d284c766c8a54fdc1318
– user60252
Nothing wrong http://sandbox.onlinephpfunctions.com/code/eb9ef3f04f2f82b00776d14263cfac68a1883759
– user60252
on another site https://ideone.com/AnF7Ik
– user60252
Valeu @Leocaracciolo did not know these sites, very useful!
– caiocafardo