Jump to content

Count matches


dk4210

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/230208-count-matches/
Share on other sites

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()

Link to comment
https://forums.phpfreaks.com/topic/230208-count-matches/#findComment-1185637
Share on other sites

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?

 

 

Link to comment
https://forums.phpfreaks.com/topic/230208-count-matches/#findComment-1185858
Share on other sites

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";

Link to comment
https://forums.phpfreaks.com/topic/230208-count-matches/#findComment-1186069
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.