Jump to content

Comparing Two Strings


TheMayhem

Recommended Posts

I'm a little stumped how to do this, so hopefully I'll find the right answer. I have two strings:

 

$customtitle and $censoredwords

 

Inside the $customtitle string is a user typed string consisting of words, letters, numbers, etc. Inside the $censoredwords are censored words or characters typed by the Administrator. Each word is seperated by a space, which is what is really confusing me.

 

My question to you is how can I compare the two strings and see if $customtitle contains any words from $censoredwords and if so kickback a TRUE or numerical value of 1 and if not kick back a FALSE or a numerical value of 0.

Link to comment
https://forums.phpfreaks.com/topic/201956-comparing-two-strings/
Share on other sites

I would create $censoredwords to an array of strings. Then use a for loop or maybe a foreach loop to compare the strings.

 

Here is some code on the spot:

$title = "mean";
$censored = array("stupid", "mean", "gay");
for($i=0; $i<count($censored); $i++){
  if($censored[$i] == $title){
      echo "naughty word";
      break;
  }
}

 

Not sure if that code is all correct, I kinda did it real quick.

Not sure if this would work or not.. I just wrote this until someone can help me with my problem :)

 

        <?php
        
        $customtitle = "This is my custom title poop";
        $customtitle = explode(" ", $customtitle);
        
        $censoredwords = "poop dang";
        $censoredwords = explode(" ", $censoredwords);
        
        foreach($customtitle as $word)
        {
            
            if(in_array($word, $censoredwords))
            {
                // do whatever here.. like flag the title as bad or whatever
                echo "Shame on you! " . $word . " is a bad word!";
                
            }
            
        }
        
        ?>

 

Obviously there's better ways to write this, but you get the idea.. oh and this is untested, but it should work.

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.