gerkintrigg Posted November 22, 2009 Share Posted November 22, 2009 Hi everyone! I wondered whether there's a way of counting the number of replacements made from a string replacement. Example: $string="I like strings. I think they are brilliant. I will get to the bottom of this issue."; $word_to_replace="I"; $replacement="We"; string=preg_replace($word_to_replace, $replacement, $string); echo $string; That would just output: We like strings. We think they are brilliant. We will get to the bottom of this issue. But what I need (strange as it might seem) is this: We1 like strings. We2 think they are brilliant. We3 will get to the bottom of this issue. I understand loops and how to increase a counting variable, but not how to do this with a preg_replace function. Any suggestions? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/182495-counting-replacements-in-strings/ Share on other sites More sharing options...
taquitosensei Posted November 22, 2009 Share Posted November 22, 2009 just add $count as the 5th string=preg_replace($word_to_replace, $replacement, $string,-1, $count); // -1 is no limit Quote Link to comment https://forums.phpfreaks.com/topic/182495-counting-replacements-in-strings/#findComment-963193 Share on other sites More sharing options...
rajivgonsalves Posted November 22, 2009 Share Posted November 22, 2009 I am not sure if this is the optimum way to do it but it kinda works <?php $counter = 1; $string="I like strings. I think they are brilliant. I will get to the bottom of this issue."; $string=preg_replace("#I#e", 'W.$counter++' , $string); echo $string; ?> Quote Link to comment https://forums.phpfreaks.com/topic/182495-counting-replacements-in-strings/#findComment-963195 Share on other sites More sharing options...
rajivgonsalves Posted November 22, 2009 Share Posted November 22, 2009 actually the code should have been, this will not throw a notice <?php $counter = 1; $string="I like strings. I think they are brilliant. I will get to the bottom of this issue."; $string=preg_replace("#I#e", '"W".$counter++' , $string); echo $string; ?> Quote Link to comment https://forums.phpfreaks.com/topic/182495-counting-replacements-in-strings/#findComment-963206 Share on other sites More sharing options...
gerkintrigg Posted November 22, 2009 Author Share Posted November 22, 2009 just add $count as the 5th string=preg_replace($word_to_replace, $replacement, $string,-1, $count); // -1 is no limit That didn't work at all... Quote Link to comment https://forums.phpfreaks.com/topic/182495-counting-replacements-in-strings/#findComment-963211 Share on other sites More sharing options...
cags Posted November 22, 2009 Share Posted November 22, 2009 What about it didn't work? Quote Link to comment https://forums.phpfreaks.com/topic/182495-counting-replacements-in-strings/#findComment-963216 Share on other sites More sharing options...
taquitosensei Posted November 22, 2009 Share Posted November 22, 2009 that's straight from phps documentation. mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] ) If specified, this variable will be filled with the number of replacements done. nm I just read your post all the way through. You'll probably have to use a callback function to increment your $replacement. Quote Link to comment https://forums.phpfreaks.com/topic/182495-counting-replacements-in-strings/#findComment-963242 Share on other sites More sharing options...
Andy-H Posted November 22, 2009 Share Posted November 22, 2009 $string="I like strings. I think they are brilliant. I will get to the bottom of this issue."; $word_to_replace="I"; $replacement="We"; string=preg_replace($word_to_replace, $replacement, $string); echo $string; It didn't work because you didn't test your example code, you missed a $ from before "string" and and "I" is not a regular expression... $string="I like strings. I think they are brilliant. I will get to the bottom of this issue."; $word_to_replace="#I#"; $replacement="We"; $string=preg_replace($word_to_replace, $replacement, $string, -1, $count); echo '<pre>' . "\r\n"; echo 'Output:' . "\t\t\t" . $string . "\r\n"; echo 'Replacements made:' . "\t" . number_format($count, 0) . "\r\n"; echo '</pre>'; Output: We like strings. We think they are brilliant. We will get to the bottom of this issue. Replacements made: 3 Quote Link to comment https://forums.phpfreaks.com/topic/182495-counting-replacements-in-strings/#findComment-963250 Share on other sites More sharing options...
gerkintrigg Posted November 22, 2009 Author Share Posted November 22, 2009 Baaa. I feel sheepish. Eventually I did this as a 2 stage approach like this: #original (case insensitive) word from database: $uc_word=ucwords($word); $upper_word=strtoupper($word); $pattern = array( '~\b'.$word.'\b(?![^<]*?>)~', '~\b'.$uc_word.'\b(?![^<]*?>)~', '~\b'.$upper_word.'\b(?![^<]*?>)~' ); #Replacement highlighted phrase: $new_word = array( '<span id="'.$word_all.'_nst_id_007" class="'.$style.'">'.$word.'</span>', '<span id="'.$word_all.'_nst_id_007" class="'.$style.'">'.$uc_word.'</span>', '<span id="'.$word_all.'_nst_id_007" class="'.$style.'">'.$upper_word.'</span>' ); #now highlight the words: $page = preg_replace($pattern, $new_word, $page); Then: $replacement='nst_id_'; $counter=1; $page=preg_replace("#nst_id_007#e", '"$replacement".$counter++', $page); Quote Link to comment https://forums.phpfreaks.com/topic/182495-counting-replacements-in-strings/#findComment-963259 Share on other sites More sharing options...
Alex Posted November 22, 2009 Share Posted November 22, 2009 You could've used substr_count Quote Link to comment https://forums.phpfreaks.com/topic/182495-counting-replacements-in-strings/#findComment-963268 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.