I want to match the Russian words using preg_match in php
i tried below examples but it is not working
"/\b".$word."\b/i"
'/(?<!\pL)'.$word.'(?!\pL)/iu'
/\b'.$word.'\b/iu
'/'.$find_ru.'/iu'
e.g
Input
$word = "королевства";
$russian_string = "источники в спецслужбах королевства";
if(preg_match('/'.$word.'/iu', $russian_string)){
echo "Matched";
}else{
echo "Not Matched";
}
Missing closing bracket in if condition
$word = "королевства";
$russian_string = "источники в спецслужбах королевства";
if(preg_match('/'.$word.'/iu', $russian_string, $word)){
echo "Matched";
}else{
echo "Not Matched";
}
Output will be "Matched"
Or you can do like this way also,
$haystack = "источники в спецслужбах королевства";
$needle = "королевства";
if( strpos( $haystack, $needle ) !== false ) {
echo "Matched";
}else{
echo "Not Matched";
}
Output will be "Matched"
Related
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.
I am trying to check for two conditions,
The string should contain a vowel.
The string should contain a space.
Here is what I am writing:
$reg = "/(?=.*(\s)) (?=.*(a|e|i|o|u))/";
But on running:
if ( preg_match($reg,"kka "))
echo "YES.";
else
echo "NO.";
I am getting NO. What am I doing wrong?
((?:.*)[aeiouAEIOU]+(?:.*)[ ]+(?:.*))|(?:.*)[ ]+((?:.*)[aeiouAEIOU]+(?:.*))
You can try with this
Explnation
Here is the correct way to use lookaheads:
^((?=.*\s.*).)((?=.*[aeiou].*).).*$
Demo here:
Regex101
If you want an option which does not involve using a regex would be to remove spaces/vowels from the input string and verify that the resulting length has decreased.
$input = "kka ";
if (strlen(preg_replace("/\s/", "", $input)) < strlen($input) &&
strlen(preg_replace("/[aeiouAEIOU]/", "", $input)) < strlen($input)) {
echo "both conditions satisfied"
else {
echo "both conditions not satisfied"
}
The alternative solution using preg_replace and strpos functions:
$str = " aa k";
if (($replaced = preg_replace("/[^aeiou ]/i", "", $str)) && strlen($replaced) >= 2
&& strpos($replaced, " ") !== false) {
echo 'Yes';
} else {
echo 'No';
}
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";
?>
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
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';
}