dk4210 Posted March 10, 2011 Share Posted March 10, 2011 Hello guys. I have the following code and I would like to count the number of times that the word has been replaced. I tried echo count ($replacement) and echo count ($match) but both only show "1". I want to be able to tell if there has been a replacement and send a warning email and/or add a flag in the db. $search_for_bad_words = mysql_query("SELECT * FROM badwords WHERE 1"); $ad_title2 = $ad_title; $ad_body2 = $ad_body; //$wordlist = "shit:cr*p|dang:d*ng|shoot:sh**t"; $seperate_text = "|"; $entry_seperate_text = ":"; while($row = mysql_fetch_array($search_for_bad_words)) { $wordlist = $wordlist.$seperate_text.$row[word].$entry_seperate_text.$row[r_word]; } $words = explode('|', $wordlist); foreach ($words as $word) { list($match, $replacement) = explode(':', $word); $ad_title2 = preg_replace("/([^a-z^A-Z]?)($match)([^a-z^A-Z]?)/i", "$1".$replacement."$3", $ad_title2); $ad_body2 = preg_replace("/([^a-z^A-Z]?)($match)([^a-z^A-Z]?)/i", "$1".$replacement."$3", $ad_body2); } Thanks for all your help! Quote Link to comment https://forums.phpfreaks.com/topic/230208-count-matches/ Share on other sites More sharing options...
HuggieBear Posted March 10, 2011 Share Posted March 10, 2011 Change these lines $ad_title2 = preg_replace("/([^a-z^A-Z]?)($match)([^a-z^A-Z]?)/i", "$1".$replacement."$3", $ad_title2); $ad_body2 = preg_replace("/([^a-z^A-Z]?)($match)([^a-z^A-Z]?)/i", "$1".$replacement."$3", $ad_body2); to these: $ad_title2 = preg_replace("/([^a-z^A-Z]?)($match)([^a-z^A-Z]?)/i", "$1".$replacement."$3", $ad_title2, -1, $title_rep); $ad_body2 = preg_replace("/([^a-z^A-Z]?)($match)([^a-z^A-Z]?)/i", "$1".$replacement."$3", $ad_body2, -1, $body_rep); Then $title_rep contains the replacement count from the title and $body_rep contains the body replacement count. Check out the docs for preq_replace() Quote Link to comment https://forums.phpfreaks.com/topic/230208-count-matches/#findComment-1185637 Share on other sites More sharing options...
dk4210 Posted March 10, 2011 Author Share Posted March 10, 2011 Hi, I replaced it and I echo'd the two vars I still get "0" for both Here is my entire code $search_for_bad_words = mysql_query("SELECT * FROM badwords WHERE 1"); $ad_title2 = $ad_title; $ad_body2 = $ad_body; //$wordlist = "shit:cr*p|dang:d*ng|shoot:sh**t"; $seperate_text = "|"; $entry_seperate_text = ":"; while($row = mysql_fetch_array($search_for_bad_words)) { $wordlist = $wordlist.$seperate_text.$row[word].$entry_seperate_text.$row[r_word]; } $words = explode('|', $wordlist); $count = 0; foreach ($words as $word) { list($match, $replacement) = explode(':', $word); //$ad_title2 = preg_replace("/([^a-z^A-Z]?)($match)([^a-z^A-Z]?)/i", "$1".$replacement."$3", $ad_title2, -1,$count); //$ad_body2 = preg_replace("/([^a-z^A-Z]?)($match)([^a-z^A-Z]?)/i", "$1".$replacement."$3", $ad_body2, -1,$count); $ad_title2 = preg_replace("/([^a-z^A-Z]?)($match)([^a-z^A-Z]?)/i", "$1".$replacement."$3", $ad_title2, -1, $title_rep); $ad_body2 = preg_replace("/([^a-z^A-Z]?)($match)([^a-z^A-Z]?)/i", "$1".$replacement."$3", $ad_body2, -1, $body_rep); } echo "This is title" . $title_rep; echo "<br>"; echo "This is body" . $body_rep; Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/230208-count-matches/#findComment-1185858 Share on other sites More sharing options...
HuggieBear Posted March 11, 2011 Share Posted March 11, 2011 Try this, I've simplified the regular expression for the purposes of the example, and I only tested it with a static array, now I've added your DB code back in, I may have made a mistake. // Get words from the database and stick them into arrays $words = mysql_query("SELECT * FROM badwords WHERE 1"); while($row = mysql_fetch_array($words)){ $badwords[] = "/" . $row['word'] . "/i"; $goodwords[] = $row['rword']; } $ad_title2 = $ad_title; $ad_body2 = $ad_body; // Replace the bad words with the good words $ad_title2 = preg_replace($badwords, $goodwords, $ad_title2, -1, $title_rep); $ad_body2 = preg_replace($badwords, $goodwords, $ad_body2, -1, $body_rep); // Echo the replacement count and the new title and body echo "Title replacements: " . $title_rep . "<br />\n"; echo "Body replacements: " . $body_rep . "<br />\n"; echo "$ad_title2<br />\n"; echo "$ad_body2<br />\n"; Quote Link to comment https://forums.phpfreaks.com/topic/230208-count-matches/#findComment-1186069 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.