How do I check if a string contains a specific word?
Now with PHP 8 you can do this using str_contains:
if (str_contains('How are you', 'are')) {
echo 'true';
}
RFC
Before PHP 8
You can use the strpos()
function which is used to find the occurrence of one string inside another one:
$a = 'How are you?';
if (strpos($a, 'are') !== false) {
echo 'true';
}
Note that the use of !== false
is deliberate (neither != false
nor === true
will return the desired result); strpos()
returns either the offset at which the needle string begins in the haystack string, or the boolean false
if the needle isn't found. Since 0 is a valid offset and 0 is "falsey", we can't use simpler constructs like !strpos($a, 'are')
.
To check if string contains particular word
Not as complicated as they say, check this you will not regret.
String sentence = "Check this answer and you can find the keyword with this code";
String search = "keyword";
if ( sentence.toLowerCase().indexOf(search.toLowerCase()) != -1 ) {
System.out.println("I found the keyword");
} else {
System.out.println("not found");
}
You can change the toLowerCase()
if you want.
How to check if a string contains a specific text
Use the strpos
function: http://php.net/manual/en/function.strpos.php
$haystack = "foo bar baz";
$needle = "bar";
if( strpos( $haystack, $needle ) !== false) {
echo "\"bar\" exists in the haystack variable";
}
In your case:
if( strpos( $a, 'some text' ) !== false ) echo 'text';
Note that my use of the !==
operator (instead of != false
or == true
or even just if( strpos( ... ) ) {
) is because of the "truthy"/"falsy" nature of PHP's handling of the return value of strpos
.
As of PHP 8.0.0 you can now use str_contains
<?php
if (str_contains('abc', '')) {
echo "Checking the existence of the empty string will always
return true";
}
FLUTTER - Checking if a string contains another one
In this case, separate the whole answer with the "space", then compare it with the correct word.
For an example:
User's answer: That is my school
Separate it with space, so that you will find an array of words:
that, is, my, school.
Then compare each word with your word. It will give you the correct answer.
The flutter code will be like below:
usersAnswer?.split(" ").forEach((word){
if(word == correctAnswer)
print("this is a correct answer");
});
How to check if a string contains specific words in javascript
You are trying to search for "registered" in a string called error but there isn't try this:
message = 'User error fabric-ca request register failed with errors [[{"code":0,"message":"Registration of \'yandgsub15\' failed: Identity \'yandgsub15\' is already registered"}]]' }
var n = message.includes("registered");
Check if a string conains a specific word
The line
echo 'amazon';
is missing a quotation mark. Note the color coding changes. That is usually caused by a missing quotation mark.
Assuming by "return" you mean having the function assign that value to a variable, all the output strings should be
return 'string';
instead of
echo 'string';
A potential additional issue is having 2 if statements instead of using else if. If you do intent to echo the strings instead of returning them, it should be like this so when it's equal to 'amazon' it doesn't also echo 'no url'
function url_mapping_name( $urlname ) {
if (str_contains($urlname, 'amazon')) {
echo 'amazon';
} else if (str_contains($urlname, 'brickset')) {
echo 'brickset';
} else {
echo 'no URL';
}
Check whether a string contains a specific word
Here is a function that returns True
if it can find a word in a string, and False
if it can't.
def isWordIn(word, string):
return not(string.find(word) == -1)
How to check if a string contain specific words?
If you are looking for exact words and don't want it to match things like "nightmare" (which is probably what you need), you can use a regex:
/\bare\b/gi
\b = word boundary
g = global
i = case insensitive (if needed)
If you just want to find the characters "are", then use indexOf
.
If you want to match arbitrary words, you have to programatically construct a RegExp (regular expression) object itself based on the word string and use test
.
Related Topics
Sum Values from Column in HTML
How to Remove Accents from Characters in a PHP String
Mongodb Updating Fields in Nested Array
(Php, Mysql) Result Could Not Be Converted to String
Generating a Truly Unique Order Id in PHP
Display Data from Database to Dropdown Codeigniter
Convert Accented Characters to Their Plain Ascii Equivalents
Get All Hrefs from String But Then Replace Via Another Method
Openssl Ssl_Read: Ssl_Error_Syscall, Errno 104
View My PHP Source Code in a Browser Via a Button
Trim Country Code from Phone Number in PHP
Saving Multiple Records in a Laravel Eloquent Create
Telegram Botapi, Send Message to Multiple Chat_Id
How to Prevent Multiple Inserts When Submitting a Form in PHP
Laravel Array to String Conversion
How to Unlink (Delete) an Image in Codeigniter
Laravel-5 How to Populate Select Box from Database With Id Value and Name Value