I am trying to check for a word in every created sting.
So I am using "if" after "if" in order to check the word in every sting but also if the word is there to print out every thing in which find it.
But i want if the searched word is not found in ANY of the strings to go to else statement No such word.
But now the code posted "no such word" even if Only ONE of the "IFs" is not ok.
Here is the code:
if(stripos($str, $text)){
/*echo $str ;*/ echo $str = preg_replace("/\b([a-z]*${fwcode}[a-z]*)\b/i","<font color ='red'><b>$1</b></font>",$str);
echo "</br>";
}
if(stripos($str2, $text)){
/*echo $str2 ;*/ echo $str2 = preg_replace("/\b([a-z]*${fwcode}[a-z]*)\b/i","<font color ='red'><b>$1</b></font>",$str2);
}
if(stripos($str3, $text)){
/*echo $str3 ;*/ echo $str3 = preg_replace("/\b([a-z]*${fwcode}[a-z]*)\b/i","<font color ='red'><b>$1</b></font>",$str3);
}
if(stripos($str4, $text)){
/*echo $str4 ;*/ echo $str4 = preg_replace("/\b([a-z]*${fwcode}[a-z]*)\b/i","<font color ='red'><b>$1</b></font>",$str4);
}
else
{
echo "No such word"; *** Now will print it if cannot find the $text in ANY of the $str -ings
}
$str,$str1,$str2... are the strings. $text-> is the variable for search.
Please tell me how to have all the strings checked and displayed if the searched word is there, and if in none of the is found Only Then to have else executed.
Thank you.
I tried to put If statement in IF but didn't work.
Please tell me how to have all the strings checked and displayed if the searched word is there, and if in none of the is found Only Then to have else executed.
Thank you.
Your else statement from your code example only applies to the last if:
$arr_string = [
'string1',
'string2',
'string3',
'string4',
];
$found = false;
foreach ($arr_string as $value) {
if(stripos($value, $text) !== false) {
echo preg_replace("/\b([a-z]*${fwcode}[a-z]*)\b/i","<font color ='red'><b>$1</b></font>",$value);
$found = true;
}
}
if (!$found) {
echo "No such word";
}
This way you can easily check as many strings as you want. Using a loop like foreach or for.
Also read about the use of stripos: https://www.php.net/manual/en/function.stripos.php
This function may return Boolean false, but may also return a
non-Boolean value which evaluates to false.
Related
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';
I am trying to check if a number say 1 is present in a string say 11,12,13 To do this I am using strpos:
<?
$s = "2,3,11,12,13";
$ss = "1";
if(strpos($s, $ss)) echo "success";
else echo "fail";
?>
Here I am expecting the code to give fail as output but it gives success.
Is there any function I can use to exactly match the number in a string. I want the number 1 in the string, not the numbers containing 1 like 11,123,100 etc.
Edit 1: It works fine for #Twinfriends answer but the problem is doing it with multiple numbers.
<?
$s = "2,3,11,12,13";
$ss = "1,2";
if(strpos($s, $ss)) echo "success";
else echo "fail";
?>
This code gives output fail but it should give true.
Edit 2: Check answer https://stackoverflow.com/a/48297002/8490014
The problem has been solved but because of for each loop the output is getting repeated as many times as there are values in the array. How can we get the output repeating the output?
You could do it like this:
<?php
$s = "2,3,11,12,13";
$ss = "1";
$toTest = explode(",", $s);
if(in_array("$ss", $toTest)) {
echo "success";
}
else {
echo "fail";
}
?>
If you've any question considering this code, feel free to ask. (Code tested, works well)
The problem has been solved by adding a foreach loop:
<?
$s = "2,3,11,12,13";
$ss = "1,2,3,5";
$catid = explode(",", $s);
$ocat = explode(",",$ss);
foreach($ocat as $abc) {
if(in_array($abc, $catid)) {
echo "success";
}
else {
echo "fail";
}
}
?>
Try this:
<?php
$s = "2,3,11,12,13";
$ss = "1";
$haystack = explode(",",$s);
$matches = preg_grep ("/$ss/", $haystack);
echo !empty($matches) ? "success" : "fail";
You can check by appending comma after the variable to check occurrence at start, appending comma before the variable to check occurrence at last or appending comma before and after the variable to check occurrence at middle.
if((substr($s, 0, (strlen($ss)+1)) === $ss.',')||(substr($s, (strlen($s)-strlen($ss)-1), strlen($s)) === ','.$ss)||(strpos($s, ','.$ss.','))){
echo "success";
}
else{
echo "fail";
}
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.
I have a comma separated list and in that list i am interested to know if a specific string that starts in a certain way is present.
$accounting = 'acc';
$str = 'loan,k,hi,588888,acc';
if (strpos($str, $accounting) === TRUE)
{
echo 'that contained accounting';
}
else{
echo 'nothing was found';
}
The code is giving me nothing is found.Does strpos work in a comma delimited list?.
Does strpos work in a comma delimited list?.
No, it doesn't, because $str is not a list, it's just a string. You have to convert it to a list (=array) first:
$lst = explode(',', $str);
and then search this list:
if(in_array('acc', $lst)....
Your wording is a bit unclear, but if you're looking for a list element that starts with a specific string, it's more complicated:
function has_element_that_starts_with($lst, $prefix) {
foreach($lst as $item)
if(strpos($item, $prefix) === 0) // note three ='s
return true;
return false;
}
Another option is a regular expression:
if(preg_match("~(^|,){$acc}(,|$)~", $str)....
for partial strings:
if(preg_match("~(^|,){$acc}~", $str)....
Your code is right change === to ==. will work.
$accounting = 'acc';
$str = 'loan,k,hi,588888,acc';
if (strpos($str, $accounting) == TRUE)
{
echo 'that contained accounting';
}
else{
echo 'nothing was found';
}
Use php strstr() , Reference
$accounting = 'acc';
$str = 'loan,k,hi,588888,acc';
if (strstr($str, $accounting) )
{
echo 'that contained accounting';
}
else{
echo 'nothing was found';
}
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.