Reputation: 435
How to invert the function of How do I check if a string contains a specific word in PHP?
if (strpos($a,'are') !== false) {
echo 'true';
}
So it echoes true
if are
is not found in $a
.
Upvotes: 33
Views: 84982
Reputation: 882
The cleanest way is to negate the response from str_contains.
if (!str_contains($var, 'something')) {
// $var does not contain 'something'
}
Upvotes: 1
Reputation: 331
I was looking for a solution for my database object and checking to see if the filename in the database object contained a "." If it did it meant all the image upload checks were successful, and then not display the div error.
<?PHP
} else if ( str_contains('$gigObject->getPhoto()','.' ) !== false) {
?>
<div class="bottom" id="photoHeaderError" style="display:block">
<div class="bottom_error_alert">
Image is missing. Please upload one for your gig.
</div>
</div>
<?php
}
?>
Upvotes: 0
Reputation: 48
function find_word($text, $word) {
/*yuHp*/
$sizeText = strlen($text);
$sizeWord = strlen($word);
$text = strtolower($text);
$word = strtolower($word);
$contadorText = 0;
$pontuacao = 0;
while ($contadorText < $sizeText) {
if ($text[$contadorText] == $word[$pontuacao]) {
$pontuacao++;
if ($pontuacao == $sizeWord) {
return true;
}
} else {
$pontuacao = 0;
}
$contadorText++;
}
return false;
}
if (!find_word('today', 'odf')){
//did not find 'odf' inside 'today'
}
Upvotes: 0
Reputation: 370
strpos() !== false gives you a wrong return value, if the search-string is at the beginning of the string. So you better use strstr(), which gives you an accurate result.
if (!strstr($mystring, 'Hello')) {
// $mystring does not contain 'Hello' nowhere,
// even not at the beginning of the string
}
else{
// Your code goes here....
}
Upvotes: 0
Reputation: 2604
The code here:
if (strpos($a, 'are') !== false) {
// The word WAS found
}
Means that the word WAS found in the string. If you remove the NOT (!) operator, you have reversed the condition.
if (strpos($a, 'are') === false) {
// The word was NOT found
}
the === is very important, because strpos will return 0 if the word 'are' is at the very beginning of the string, and since 0 loosely equals FALSE, you would be frustrated trying to find out what was wrong. The === operator makes it check very literally if the result was a boolean false and not a 0.
As an example,
if (!strpos($a, 'are')) {
// String Not Found
}
This code will say the string 'are' is not found, if $a = "are you coming over tonight?", because the position of 'are' is 0, the beginning of the string. This is why using the === false check is so important.
Upvotes: 76
Reputation: 17208
Using strstr():
if (!strstr($var, 'something')) {
// $var does not contain 'something'
}
Or strpos():
if (strpos($var, 'something') === false) {
// $var does not contain 'something'
}
Or stripos() if you want case-insensitive search.
strpos() is a little faster
Upvotes: 7
Reputation: 41
Try this
$string = "This is beautiful world.";
$$string = "Beautiful";
preg_match('/\b(express\w+)\b/', $string, $x); // matches expression
\b is a word boundary
\w+ is one or more "word" character
\w* is zero or more "word" characters
enter code here
See the manual on escape sequences for PCRE.
Upvotes: 0
Reputation: 6202
You'll probably kick yourself when you see it...
if (!strpos($a,'are') !== false) {
echo 'true';
}
Upvotes: 3