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

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';

Related

PHP issue with IF statement

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.

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.

how do i check for a string occurence in input data

I want to check for occurence of a particular string in user input data.
How do I do it.
Here's my code:
<?php
$data = $_POST["data"];
if (strcmp("123",$data))
echo "string matches";
else if (strcmp("234",$data))
echo "string 2 matches";
else
echo"string does not match";
?>
If you want to check to see if $_POST['data'] contains the strings you're searching for, do this:
<?php
$data = $_POST["data"];
if (strpos($data, "123") !== false)
echo "data contains 123";
else if (strpos($data, "234") !== false)
echo "data contains 234";
else
echo "data does not contain either";
But, if you want to check for an exact match, you'd just do:
<?php
//...
if ($data == "123")
echo "data is equal 123";
Check out the manual for strpos() for more information. For a case-insensitive search, you can use stripos() (i for "insensitive") instead.
Checking for strict equality with false is important. If the match begins at the first character in the string, it will return 0, which indicates a match. You need !== to distinguish 0 from false.
Try this,
<?php
//$data = isset($_POST["data"])?$_POST["data"]:'';
$data = "12356789";
if (strpos($data, "123")!== false){
echo "string matches";
}
else if (strpos($data, "234")!== false){
echo "string 2 matches";
} else {
echo"string does not match";
}
?>
Try this one:
<?php
$data = $_POST["data"];
if (strcmp("123",$data) === 0)
echo "string matches";
else if (strcmp("234",$data))
echo "string 2 matches";
else
echo"string does not match";
?>

Searching for occurence of a string in a comma delimited list

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';
}

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.

Categories