There are a lot of options here I believe.
Here is my solution(s): You could use search_array()
for this. The search array returns false if no other needle is found in_array in an array. If another word is found it returns the key.
Depending of your needs, you could use one of these options below.
//Option 1
//Words that actually appear more than once...
$new_arr = array();
foreach($exp as $key=>$e) {
//Must be this word only (therefore the true-statement
$search = array_search($e, $exp, true);
if ($search !== false && $search != $key) {
$new_arr[] = $e;
}
}
//Option 2
//
//Your question was not totally clear so I add this code as well
//Words with asterixes before and after that appear more than once
$new_arr = array();
foreach($exp as $key=>$e) {
//Two asterixes at the beginning of the sting and two at the end
//strtolower sets **Technology** and **technology** as a duplicate of word
if (substr($e,0,2) == "**" && substr($e,-2,2) == "**") {
$search = array_search(strtolower($e), $exp);
if ($search !== false && $search != $key) {
$new_arr[] = $e;
}
}
}
for($j = 0; $j < count($new_arr); $j++){
$this->db->query("INSERT INTO news (news_id, news_content)
VALUES ('$id', $new_arr[$j])");
}
As someone mentioned in a comment you should prevent SQL injections by typing that way in the INSERT-statement (and you should), but the question was mainly about finding duplicates in a string in do something with them so therefore I won't go further with that comment.
The result array $new_arr
would like: (option 1)
array (size=9)
0 => string 'the' (length=3)
1 => string 'the' (length=3)
2 => string '**food**' (length=8)
3 => string 'to' (length=2)
4 => string 'the' (length=3)
5 => string '**impact**' (length=10)
6 => string 'have' (length=4)
7 => string 'on' (length=2)
8 => string 'the' (length=3)
The reason why Technology and technology is not the same because that its an uppercase T in one of the words.
The result array $new_arr
would like: (option 2)
array (size=3)
0 => string '**food**' (length=8)
1 => string '**Technology**' (length=14)
2 => string '**impact**' (length=10)