Jump to content

Recommended Posts

going off this thread

http://www.phpfreaks.com/forums/index.php/topic,225600.15.html

 

I was wondering if there is a way to make the expression used here case insensitive

<?php
$comment = 'Dang this crap, I want to shoot somebody.  But that would be dangerous!';
$wordlist = "crap:cr*p|dang:d*ng|shoot:sh**t";
$words = explode('|', $wordlist);
foreach ($words as $key=>$word) {
   list($needle[$key],$replacement[$key])=explode(':', $word);
   $needle[$key]= "/\b{$needle[$key]}\b/i";
}
$comment = preg_replace($needle,$replacement, $comment);
echo $comment;
?>

so that crap replaces with cr*p and Crap replaces with Cr*p or CRAP replaces with CR*P and so on

 

//terrible at regex

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/134936-make-this-case-insensitive/
Share on other sites

Okay first off, dunno where that word list is coming from.  But it looks like you are just putting *'s where the vowels are, so I don't really see the point in you keeping a word=>replacement combo like that.  I'd just keep a list of the words and use some regex to replace the vowels with *'s. 

 

2nd...okay I'm sure there is probably a way better way to do this, but uh...yeah, there you have it.

 

<?php
// example string
$comment = 'Dang this Crap, I want to DaNG shoot somebody. crAp  But that would be dangerous!';
// array of naughty words
$wordlist = array('dang','crap','shoot');

// for each word in the naughty list...
foreach ($wordlist as $word) {
   // see if there is a match in $comment
   preg_match_all("/\b{$word}\b/i", $comment, $m);
   // if there is, add it to list of matches
   $matches .= "," . implode(',',$m[0]);
}
// trim off the leading comma from the loop
$matches = ltrim($matches, ',');
// explode it into an array. Now you have an array of all the bad words in $comment, case preserved.
$matches = explode(',',$matches);

// for each badword found...
foreach ($matches as $w) {
   // replace the vowels with a *, but make it a new array, so as to preserve the original words
   $replacement[] = preg_replace("/[aeiou]/i","*",$w);
}

// for each badword found in $comment...
foreach($matches as $key => $val)    {
   // replace the word with the replacement
   $comment = preg_replace("/{$val}/", $replacement[$key], $comment);
} 
echo $comment;
?>

here I even condensed it some more...

 

<?php
$comment = 'Dang this Crap, I want to DaNG shoot somebody.crAp  But that would be dangerous!';
$wordlist = array('dang','crap','shoot');
foreach ($wordlist as $word) {
   preg_match_all("/\b{$word}\b/i", $comment, $m);
   foreach($m[0] as $val) {
      $replacement = preg_replace("/[aeiou]/i","*",$val);
      $comment = preg_replace("/{$val}/", $replacement, $comment);
   }
}
echo $comment;
?>

The vowel code seems simple enough, although to demonstrate how you might do this with regex functions alone:

<?php
$sourcestring="Dang this crap, I want to SHOOT somebody.  But that would be dangerous!";
echo preg_replace('/(?<=\bCR)A(?=P\b)|(?<=\bSH)OO(?=T\b)/ie','preg_replace(\'/./\',\'*\',\'\0\')',$sourcestring);
?>

guys, thanks for the responses, the word list as i now see does look like it is just replacing the vowels, but it is intended to block any word I want with any string i want so crap could become c*** if i deemed cr*p to be too revealing (obviously using a word with a much higher offensiveness) I could even change crap into ***REMOVED*** (which i guess takes out the case sensitivity)

 

but what i really want is for me to add the problem word once to the wordlist, and then the preg function to find the word but retain the case of the letters that are not changed so that the comment (that is what this is for) would not be altered too much

 

So If I wErE tO tYpE cRaP lIkE tHiS it would turn into So If I wErE tO tYpE cR*P lIkE tHiS

thus retaining the style of the author but filtering the bad words

Okay, $wordlist is made up of word=>filter.  If the filter is the same length as the word, it will do a case-insensitive replacement on the bad word.  If it is not the same length, it will replace the bad word with the filter, regardless of case.  That is because it is assumed that if the filter is not the same length as the word, you are wanting it replaced with some kind of message like *** removed *** or something.  Also, the only way the case-insensitive replacement will even work, is if it's the same length. 

 

So for example on word 'dang'

filter -> match = result

d*ng -> DANG = D*NG

***g -> DANG = ***G

d**g -> DANG = D**G

d*    -> DANG = d*

d*** -> DANG = D***

d**** -> DANG = d****

** SOME MESSAGE ** -> DANG = ** SOME MESSAGE

 

You don't have to use *'s you can use whatever you want.  You can even use a mix

 

filter -> match = result

d&&g -> danG = d&&G

&*nG -> DanG = &*nG

dnag -> DANG = DnaG

gnad -> DANG = gnad

darn -> DANG = DArn

 

<?php

$comment = 'Dang this Crap, I want to DaNG shoot somebody. crAp  But that ShooT DARN would be dangerous!';
$wordlist = array('dang' => 'd*ng','crap' => 'c**p','shoot' => 's****', 'darn' => '** REMOVED **');

foreach ($wordlist as $word => $filter) {
   preg_match_all("/\b{$word}\b/i", $comment, $matches);
   foreach ($matches[0] as $bword) {
      if (strlen($filter) == strlen($bword)) {
         $replacement = '';
         $count = strlen($filter);
         for ($pos = 0; $pos < $count; $pos++) {
            $replacement .= (strcasecmp($filter{$pos},$bword{$pos}) == 0)? $bword{$pos} : $filter{$pos}; 
         } // end for pos
      } else {
         $replacement = $filter;
      } // end if strlen
      $comment = preg_replace("/{$bword}/", $replacement, $comment);
   } // end foreach matches
} // end foreach wordlist

echo $comment;
?>

I've tired hard... at least an hour, I came up with this... I just can't get it to work with the word "Dang", maybe someone can build off it...

 

<?php
$comment = 'Dang this crap, I want to shoot somebody. But that would be dangerous!';
$wordlist = array("crap","dang","shoot");
$rword = array("a","e","i","o","u");
$replace = explode("|",implode("|",preg_replace("/(".implode("|",$rword).")/i","*",$wordlist)));
$find = explode('|','~'.implode('\b~|~',$wordlist).'\b~');
$cout = preg_replace($find,$replace,$comment);
echo $cout;
?>

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.