i have to find the occurence of each letter in a string.for example,see the below given string $string="have a wonderful day for entirely different people"; i have find occurence of each letter in string if letter occurs more than two times in a word,then remove that word from string.if anybody knows please help.
<?php
$string="have a wonderful day for entirely different people,sss greeetings";
$s= str_split($string,1);
array_count_values($s);
?>
Try this function to remove all words with more than 1 common letter, this will also keep the string structure like commas.
$string = "have a wonderful day for entirely different people,sss greeetings";
echo removeDuplicateLetterWords($string);
function removeDuplicateLetterWords($str) {
//convert all words in string to an array
$words = preg_split("/[\s,]+/", $str);
//declare an array that will be filled with words to remove
$removeWords = array();
//loop through each word
foreach($words as $word) {
//split word into an array of letters
$letters = str_split($word);
//loop through all letters
foreach($letters as $letter) {
//checks word to see if it has more than 1 of any letter
if(substr_count($word, $letter) > 1) {
//if word has multiple of a letter, add it to the "removeWords" array
//if it's not already added
if(!in_array($word, $removeWords)) $removeWords[] = $word;
}
}
}
//loop through removeWords array
foreach($removeWords as $value) {
//replace all of removeWords with an empty string
$str = str_replace($value, "", $str);
}
//return string with none of the words d from removeWords
return $str;
}
Here is the function without the comments as they were only there to explain to OP what was going on :P
function removeDuplicateLetterWords($str) {
$words = preg_split("/[\s,]+/", $str);
$removeWords = array();
foreach($words as $word) {
$letters = str_split($word);
foreach($letters as $letter) {
if(substr_count($word, $letter) > 1) {
if(!in_array($word, $removeWords))
$removeWords[] = $word;
}
}
}
foreach($removeWords as $value) {
$str = str_replace($value, "", $str);
}
return $str;
}
Side note : This counts anything that is not a letter as a word delimiter.
Here is an example to help you get started. https://iconoun.com/demo/temp_user.php
<?php // demo/temp_user.php /** * Substrings and character counts * * https://stackoverflow.com/questions/45421608/count-each-letter-in-a-string-and-letter-frequency-is-greater-than-2-then-remove */ error_reporting(E_ALL); echo '<pre>'; // ORIGINAL TEST DATA $str = "have a wonderful day for entirely different people,sss greeetings"; // NORMALIZE COMMA TO MATCH BLANK SEPARATORS $str = str_replace(',', ' ', $str); // AN ARRAY OF WORDS $arr = explode(' ', $str); // EXAMINE EACH WORD, REMOVE THOSE WITH MORE THAN TWO DUPLICATED LETTERS foreach ($arr as $key => $wrd) { $cca = count_chars($wrd, 1); foreach ($cca as $chr) { if ($chr > 2) unset($arr[$key]); } } // RECONSTRUCT $new = implode(' ', $arr); // SHOW THE WORK PRODUCT echo PHP_EOL . $str; echo PHP_EOL . $new;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.