Program to replace a word with asterisks in a sentence | Function takes two parameter ; Break down sentence by ' ▁ ' spaces and store each individual word in a different list ; A new string to store the result ; Creating the censor which is an asterisks " * " text of the length of censor word ; count variable to access our word_list ; Iterating through our list of extracted words ; changing the censored word to created asterisks censor ; join the words ; Driver code
< ? php function censor ( $ text , $ word ) { $ word_list = explode ( " ▁ " , $ text ) ; $ result = ' ' $ stars = " " ; for ( $ i = 0 ; $ i < strlen ( $ word ) ; $ i ++ ) $ stars . = " * " ; $ count = 0 ; $ index = 0 ; for ( $ i = 0 ; $ i < sizeof ( $ word_list ) ; $ i ++ ) { if ( $ word_list [ $ i ] == $ word ) $ word_list [ $ index ] = $ stars ; $ index += 1 ; } return implode ( ' ▁ ' , $ word_list ) ; } $ extract = " GeeksforGeeks ▁ is ▁ a ▁ computer ▁ science ▁ " . " portal for geeks . I am pursuing my " . STRNEWLINE TABSYMBOL TABSYMBOL TABSYMBOL TABSYMBOL TABSYMBOL " major in computer science . " $ cen = " computer " ; echo censor ( $ extract , $ cen ) ; ? >