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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.