therocker Posted November 16, 2013 Share Posted November 16, 2013 Hello, how would you count the number of characters in a string and then replacing the string with astrics? So some examples are if I have a list of 6 strings like this morning, noon, afternoon, evening, night, dawn each string gets their own astrics So for morning, there's 7 characters in that string. Then replace that word with 7 astrics like so. ******* And then the same with the rest. Noon has 4 characters so replace it with 4 astrics **** I have this as of right now which is my only code that counts characters. I would like someone to help me fix it and modify it. <?php print(strlen($row['original_row_goes_here'])); ?> Link to comment https://forums.phpfreaks.com/topic/283963-counting-characters-in-a-string-and-replacing-it/ Share on other sites More sharing options...
Ch0cu3r Posted November 16, 2013 Share Posted November 16, 2013 You'd add those words to an array, then loop through them, get the words length using strlen(). Then use str_repeat() to create a string of astrics that is the length of the word to filter. Something like foreach($filter_words as $word) { $replacement = str_repeat('*', strlen($word)); $text = str_replace($word, $replacement, $text); } Link to comment https://forums.phpfreaks.com/topic/283963-counting-characters-in-a-string-and-replacing-it/#findComment-1458539 Share on other sites More sharing options...
therocker Posted November 16, 2013 Author Share Posted November 16, 2013 You'd add those words to an array, then loop through them, get the words length using strlen(). Then use str_repeat() to create a string of astrics that is the length of the word to filter. Something like foreach($filter_words as $word) { $replacement = str_repeat('*', strlen($word)); $text = str_replace($word, $replacement, $text); } Thanks this helped. Had to do a little modifing, but I got it. Thanks. $filter_words = array('morning', 'noon', 'afternoon', 'evening', 'night', 'dawn'); foreach($filter_words as $w => $word) { $replacement = str_repeat('*', strlen($word)); $text = str_replace($word, $replacement, $word); echo $text; } Link to comment https://forums.phpfreaks.com/topic/283963-counting-characters-in-a-string-and-replacing-it/#findComment-1458564 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.