Using PHP stripos() in Logical Filtering - php

How can i use the stripos to filter out unwanted word existing on itself.
How do i twist the code below that a search for 'won' in grammar will not return true, since 'wonderful' is another word itself.
$grammar = 'it is a wonderful day';
$bad_word = 'won';
$res = stripos($grammar, $bad_word,0);
if($res === true){
echo 'bad word present';
}else{
echo 'no bad word';
}
//result 'bad word present'

Use preg_match
$grammar = 'it is a wonderful day';
$bad_word = 'won';
$pattern = "/ +" . $bad_word . " +/i";
// works with one ore more spaces around the bad word, /i means it's not case sensitive
$res = preg_match($pattern, $grammar);
// returns 1 if the pattern has been found
if($res == 1){
echo 'bad word present';
}
else{
echo 'no bad word';
}

$grammar = 'it is a wonderful day';
$bad_word = 'won';
/* \b \b indicates a word boundary, so only the distinct won not wonderful is searched */
if(preg_match("/\bwon\b/i","it is a wonderful day")){
echo "bad word was found";}
else {
echo "bad word not found";
}
//result is : bad word not found

Related

How to check if a fixed string contain any additional character?

This code check if "have" is present inside the string, assuming the string always begin with "I have found" what i need is a function that check if the string contain "I have found" plus something else. Example: I have found 500. where 500 can be anything, and unknow.
$a = 'I have found';
if (strpos($a, 'have') !== false) {
echo 'true';
}
If you want to know what has been found:
function get_found($str){
if(strpos($str, "I have found")===false)
return "nothing";
$found = trim(substr($str, strlen("I have found")));
if($found == "")
return "nothing";
return $found;
}
echo get_found("I have found a friend"); //outputs "a friend"
echo get_found("I have found"); //outputs "nothing"
You can use preg_match(), like in this code:
$a = 'I have found'; //fixed string
$str = 'I have found 500';
if (preg_match('/^'.$a.'(.+?)$/', $str, $m)){
echo 'The string contains additional: '.$m[1];
}
else echo 'String fixed';

Find words in text, php

How to resolve this problem:
Write a PHP program that finds the word in a text.
The suffix is separated from the text by a pipe.
For example: suffix|SOME_TEXT;
input: text|lorem ips llfaa Loremipsumtext.
output: Loremipsumtext
My code is this, but logic maybe is wrong:
$mystring = fgets(STDIN);
$find = explode('|', $mystring);
$pos = strpos($find, $mystring);
if ($pos === false) {
echo "The string '$find' was not found in the string '$mystring'.";
}
else {
echo "The string '$find' was found in the string '$mystring',";
echo " and exists at position $pos.";
}
explode() returns an array, so you need to use $find[0] for the suffix, and $find[1] for the text. So it should be:
$suffix = $find[0];
$text = $find[1];
$pos = strpos($text, $suffix);
if ($pos === false) {
echo "The string '$suffix' was not found in '$text'.";
} else {
echo "The string '$suffix' was found in '$text', ";
echo " and exists at position $pos.";
}
However, this returns the position of the suffix, not the word containing it. It also doesn't check that the suffix is at the end of the word, it will find it anywhere in the word. If you want to match words rather than just strings, a regular expression would be a better method.
$suffix = $find[0];
$regexp = '/\b[a-z]*' . $suffix . '\b/i';
$text = $find[1];
$found = preg_match($regexp, $text, $match);
if ($found) {
echo echo "The suffix '$suffix' was found in '$text', ";
echo " and exists in the word '$match[0]'.";
} else {
echo "The suffix '$suffix' was not found in '$text'.";
}

Match one or more keywords defined in array [duplicate]

Lets say I have an array of bad words:
$badwords = array("one", "two", "three");
And random string:
$string = "some variable text";
How to create this cycle:
if (one or more items from the $badwords array is found in $string)
echo "sorry bad word found";
else
echo "string contains no bad words";
Example:
if $string = "one fine day" or "one fine day two of us did something", user should see sorry bad word found message.
If $string = "fine day", user should see string contains no bad words message.
As I know, you can't preg_match from array. Any advices?
How about this:
$badWords = array('one', 'two', 'three');
$stringToCheck = 'some stringy thing';
// $stringToCheck = 'one stringy thing';
$noBadWordsFound = true;
foreach ($badWords as $badWord) {
if (preg_match("/\b$badWord\b/", $stringToCheck)) {
$noBadWordsFound = false;
break;
}
}
if ($noBadWordsFound) { ... } else { ... }
Why do you want to use preg_match() here?
What about this:
foreach($badwords as $badword)
{
if (strpos($string, $badword) !== false)
echo "sorry bad word found";
else
echo "string contains no bad words";
}
If you need preg_match() for some reasons, you can generate regex pattern dynamically. Something like this:
$pattern = '/(' . implode('|', $badwords) . ')/'; // $pattern = /(one|two|three)/
$result = preg_match($pattern, $string);
HTH
If you want to check each word by exploding the string into words, you can use this:
$badwordsfound = count(array_filter(
explode(" ",$string),
function ($element) use ($badwords) {
if(in_array($element,$badwords))
return true;
}
})) > 0;
if($badwordsfound){
echo "Bad words found";
}else{
echo "String clean";
}
Now, something better came to my mind, how about replacing all the bad words from the array and check if the string stays the same?
$badwords_replace = array_fill(0,count($badwords),"");
$string_clean = str_replace($badwords,$badwords_replace,$string);
if($string_clean == $string) {
echo "no bad words found";
}else{
echo "bad words found";
}
Here is the bad word filter I use and it works great:
private static $bad_name = array("word1", "word2", "word3");
// This will check for exact words only. so "ass" will be found and flagged
// but not "classic"
$badFound = preg_match("/\b(" . implode(self::$bad_name,"|") . ")\b/i", $name_in);
Then I have another variable with select strings to match:
// This will match "ass" as well as "classic" and flag it
private static $forbidden_name = array("word1", "word2", "word3");
$forbiddenFound = preg_match("/(" . implode(self::$forbidden_name,"|") . ")/i", $name_in);
Then I run an if on it:
if ($badFound) {
return FALSE;
} elseif ($forbiddenFound) {
return FALSE;
} else {
return TRUE;
}
Hope this helps. Ask if you need me to clarify anything.

preg_match array items in string?

Lets say I have an array of bad words:
$badwords = array("one", "two", "three");
And random string:
$string = "some variable text";
How to create this cycle:
if (one or more items from the $badwords array is found in $string)
echo "sorry bad word found";
else
echo "string contains no bad words";
Example:
if $string = "one fine day" or "one fine day two of us did something", user should see sorry bad word found message.
If $string = "fine day", user should see string contains no bad words message.
As I know, you can't preg_match from array. Any advices?
How about this:
$badWords = array('one', 'two', 'three');
$stringToCheck = 'some stringy thing';
// $stringToCheck = 'one stringy thing';
$noBadWordsFound = true;
foreach ($badWords as $badWord) {
if (preg_match("/\b$badWord\b/", $stringToCheck)) {
$noBadWordsFound = false;
break;
}
}
if ($noBadWordsFound) { ... } else { ... }
Why do you want to use preg_match() here?
What about this:
foreach($badwords as $badword)
{
if (strpos($string, $badword) !== false)
echo "sorry bad word found";
else
echo "string contains no bad words";
}
If you need preg_match() for some reasons, you can generate regex pattern dynamically. Something like this:
$pattern = '/(' . implode('|', $badwords) . ')/'; // $pattern = /(one|two|three)/
$result = preg_match($pattern, $string);
HTH
If you want to check each word by exploding the string into words, you can use this:
$badwordsfound = count(array_filter(
explode(" ",$string),
function ($element) use ($badwords) {
if(in_array($element,$badwords))
return true;
}
})) > 0;
if($badwordsfound){
echo "Bad words found";
}else{
echo "String clean";
}
Now, something better came to my mind, how about replacing all the bad words from the array and check if the string stays the same?
$badwords_replace = array_fill(0,count($badwords),"");
$string_clean = str_replace($badwords,$badwords_replace,$string);
if($string_clean == $string) {
echo "no bad words found";
}else{
echo "bad words found";
}
Here is the bad word filter I use and it works great:
private static $bad_name = array("word1", "word2", "word3");
// This will check for exact words only. so "ass" will be found and flagged
// but not "classic"
$badFound = preg_match("/\b(" . implode(self::$bad_name,"|") . ")\b/i", $name_in);
Then I have another variable with select strings to match:
// This will match "ass" as well as "classic" and flag it
private static $forbidden_name = array("word1", "word2", "word3");
$forbiddenFound = preg_match("/(" . implode(self::$forbidden_name,"|") . ")/i", $name_in);
Then I run an if on it:
if ($badFound) {
return FALSE;
} elseif ($forbiddenFound) {
return FALSE;
} else {
return TRUE;
}
Hope this helps. Ask if you need me to clarify anything.

PHP preg_match? How to return not matched characters?

Lets say i have:
$string = 'qwe1ASD#';
if(preg_match('/^[a-zA-Z]+$/', $string))
{
echo 'OK';
}
else
{
echo 'BAD';
}
Now, is there simple solution, to find all characters from $string which don't match expression? So in return, in place of "BAD" i want to have ex. "BAD. You can't use following characters: 1#"
Thanks in advance for any simple hints! :)
Thank you Floern, your answer suit best my needs. It have only one "preg" so it's also good for performance. Thank you again.
I implemented it for now as follw:
if(preg_match_all('/[^a-zA-Z0-9]/s', $string, $forbidden))
{
$forbidden = implode('', array_unique($forbidden[0]));
echo 'BAD. Your string contains forbidden characters: '.htmlentities($forbidden).'';
}
$tmpstring=preg_replace('~[A-Za-z]~','',$string);
if(strlen($tmpstring))
//bad chars: $tmpstring
You could use preg_match_all():
if(preg_match_all('/[^a-zA-Z]/s', $string, $matches)){
var_dump($matches); // BAD
}
else{
echo 'OK';
}
$string = 'qwe1ASD#';
if(preg_match('/^[a-zA-Z]+$/', $string))
{
echo 'OK';
}
else
{
echo 'BAD. You cannot use the following characters: ' + preg_replace('/[a-zA-Z]/', '', $string);
}
There are different ways. I find this one nice:
$check = preg_prelace('/[a-zA-Z]/', '', $string);
if ($check) echo 'BAD ' . $check;
UPDATE:
if (strlen($check)) echo 'BAD ' . $check;

Categories