Let’s See How We Can Make This?
You need to create a function to remove the word from two different strings, You can name the function as you like. We are used removeCommonWords to built this.
We will create two different variables in which we will pass the words and this function will give you the same word after removing it as you can see in the example given below:
Example Code:
<?php
function removeCommonWords($string1, $string2) {
$array1 = explode(‘ ‘, $string1);
$array2 = explode(‘ ‘, $string2);
// Find common words and remove them from both arrays
$commonWords = array_intersect($array1, $array2);
$array1 = array_diff($array1, $commonWords);
$array2 = array_diff($array2, $commonWords);
// Reconstruct strings
$newString1 = implode(‘ ‘, $array1);
$newString2 = implode(‘ ‘, $array2);
return [$newString1, $newString2];
}
// Example usage
$string1 = “Testing”;
$string2 = “Testing Data”;
list($newString1, $newString2) = removeCommonWords($string1, $string2);
echo “String 2 without common words: $newString2\n”;
?>
Copy
Use the above code for you upcoming product. This is very useful when we want to remove the same words from the different string.
Why We Use Remove The Duplicate Words In Two Different Strings ?
When we work with backend functionality like MYSQLI and retrieve the data and don’t want to show the duplicate word, Then we can use the function to remove the duplicate words.
You can also use this function to remove the same heading in two different sentences.
What is Important of Remove Duplicate words from the Two String
- Date cleanness : If the text in the database is duplicated, Then we are unable to read it properly, Which makes it very difficult to read. If we clean up the duplicate data, So it will be easier for us to read it.
- Improved Readability: If the text is not duplicate or unnecessary then it becomes easier to read and can be understood easily.
- Better User Experience: If repeated words are removed then it will be easier for the user to read