How to check if a string contains a specific text [duplicate] - php

This question already has answers here:
How do I check if a string contains a specific word?
(36 answers)
Closed 9 years ago.
<?php
$a = '';
if($a exist 'some text')
echo 'text';
?>
Suppose I have the code above, how to write the statement if($a exist 'some 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";
}

Empty strings are falsey, so you can just write:
if ($a) {
echo 'text';
}
Although if you're asking if a particular substring exists in that string, you can use strpos() to do that:
if (strpos($a, 'some text') !== false) {
echo 'text';
}

http://php.net/manual/en/function.strpos.php I think you are wondiner if 'some text' exists in the string right?
if(strpos( $a , 'some text' ) !== false)

If you need to know if a word exists in a string you can use this. As it is not clear from your question if you just want to know if the variable is a string or not. Where 'word' is the word you are searching in the string.
if (strpos($a,'word') !== false) {
echo 'true';
}
or use the is_string method. Whichs returns true or false on the given variable.
<?php
$a = '';
is_string($a);
?>

You can use strpos() or stripos() to check if the string contain the given needle. It will return the position where it was found, otherwise will return FALSE.
Use the operators === or `!== to differ FALSE from 0 in PHP.

You can use the == comparison operator to check if the variable is equal to the text:
if( $a == 'some text') {
...
You can also use strpos function to return the first occurrence of a string:
<?php
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);
// Note our use of ===. Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
echo "The string '$findme' was not found in the string '$mystring'";
} else {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
}
See documentation

Related

php strpos() unable to find bracket in string [duplicate]

This question already has answers here:
strpos issue with 0==false?
(3 answers)
Closed 1 year ago.
When I use strpos(), it isn't able to locate "[" if that bracket is the first character in the string. How can I overcome this issue and why is this happening?
$name = "[johnny";
if(strpos($name, "[") != false){
echo "Bracket found!";}else{
echo "Not found";
}
In the above code, I get "Not found" when it shouldn't be the case.
$name = "jo[hnny";
if(strpos($name, "[") != false){
echo "Bracket found!";}else{
echo "Not found";
}
But this case correctly returns "Bracket found!"
You must use !== false (double equals sign)
From the strpos manual page:
This function may return Boolean false, but may also return a non-Boolean value which evaluates to false. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
And the example given on the page:
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);
// The !== operator can also be used. Using != would not work as expected
// because the position of 'a' is 0. The statement (0 != false) evaluates
// to false.
if ($pos !== false) {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
} else {
echo "The string '$findme' was not found in the string '$mystring'";
}

How to Check if the String is present in another String in any format?

I want to check if the string is present in another string
even if the string is in this format:
are,
ARE,
Are,
aRe,
arE
$str = "how aRe you doing";
if (strpos($str,'are') !== false)
{
echo 'true';
}
Use stripos() instead of strpos().
int stripos ( string $main_string , string $substring [, int $offset = 0 ] )
You can use strpos function with strtolower function
$str = "how are you doing";
$ndl = 'ArE';
if (strpos(strtolower($str),strtolower($ndl))) {
echo "true";
}else{
echo "false";
}
$str = "how aRe you doing";
if (strpos(strtolower($str),'are') !== false)
{
echo 'true';
}

php problem: strpos function not working

why is the following php code not working:
$string = "123";
$search = "123";
if(strpos($string,$search))
{
echo "found";
}else{
echo "not found";
}
as $search is in $string - shouldn't it be triggered as found?
This is mentioned in the Manual: strpos()
This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
In your case the string is found at the index 0 and in php 0 == false
The solution is to just use the strict comparator
echo strpos($string,$search) === false
? "not found"
: "found";
Another one
echo is_int(strpos($string,$search))
? "found"
: "not found";
Or something ... lets say interesting :D Just for illustration. I don't recommend this one.
echo strpos('_' . $string,$search) // we just shift the string 1 to the right
? "found"
: "not found";
This is happening because the search string is being found at position 0.
Try
if(strpos($string,$search) !== FALSE)
instead of
if(strpos($string,$search))
strpos returns the first offset where $search was found - 0. 0 in turn evaluates to false. Therefore the if fails.
If $search was not found, strpos returns FALSE. First check the return value for !== FALSE, and then check the offset.
Thanks to everyone who pointed this out in the comments.
see: http://php.net/manual/en/function.strpos.php
From the manual:
This function may return Boolean
FALSE, but may also return a
non-Boolean value which evaluates to
FALSE, such as 0 or "". Please read
the section on Booleans for more
information. Use the === operator
for testing the return value of this
function.
In your example, you should use
$string = "123";
$search = "123";
if ( false !== strpos( $string, $search ) ) {
echo "found";
} else {
echo "not found";
}
strpos returns the numeric position of the string you want to search for if it finds it. So in your case, you want to be doing this instead:
$search = "123";
$string = "123";
if (strpos($string,$search)===false) { echo "not found"; }
else { echo "found"; }
basically it returns a false if it doesn't find your string
You can use this:
<?php
$string = "123";
$find = "123";
$strpos = strpos($string, $find);
if($strpos || $strpos === (int)0) {
echo "Found it!";
} else {
echo "Not Found!";
}
?>
Well documented issue explained here. strpos is simply returning '0'

is there an "if in" operator in PHP?

i was looking to something that checks whether a string exists inside another, as in python:
print "a" in "aloha"
wold return 1
strpos()
$pos = strpos("aloha", "a");
EDIT
You can verify the existence of string by putting IF like this:-
if (strpos("aloha", "a") !== false) {
echo "The string was found";
} else {
echo "The string was not found";
}
strpos("aloha", "a") !== false
Returns true is the letter a is in the word aloha.
Note: Its important to use !== and not != as in PHP 0 == false.
try
$mystring = 'aloha';
$findme = 'a';
$pos = strpos($mystring, $findme);
echo $pos // return 0
if ($pos === false) {
echo "The string '$findme' was not found in the string '$mystring'";
} else {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
}
You can use the srtpos() function to check if the position of a string inside another is not false -- i.e. if the string contains the other :
if (strpos("aloha", "a") !== false) {
// a is contained in aloha
}
The first parameter being the haystack -- the string that could contain what you are searching ; and the second parameter being the needle -- the string you are searching.
As noted in the manual (quoting) :
This function may return Boolean
FALSE, but may also return a
non-Boolean value which evaluates to
FALSE, such as 0 or "".
Please read the section on
Booleans for more information.
Use the === operator for
testing the return value of this
function.
If you want the search to be case-insentive, you'll use the stripos() function, btw.

how can I do a string check inside a string in php?

Does anyone know how can I do a string check inside a string?
for example:
$variable = "Pensioner (other)";
If I want to check whether $variable contain the word 'Pensioner', how can I do it in PHP? I have tried the following code in php, but it's always return me false :(
$pos = strripos($variable,"Pensioner");
if($pos) echo "found one";
else echo "not found";
In the manual, the example uses a === for comparison. The === operator also compares the type of both operands. To check for 'not equal', use !==.
Your search target 'Pensioner' is at position 0, and the function returns 0, which equal false, hence if ($pos) failed all the time. To correct that, your code should read:
$pos = strripos($variable,"Pensioner");
if($pos !== false) echo "found one";
else echo "not found";
Update:
You are using the reverse function strripos, you need to use stripos.
if (stripos($variable, "Pensioner") !== FALSE){
// found
}
else{
// not found
}
This should do:
if (strripos($variable, "Pensioner") !== FALSE){
// found
}
else{
// not found
}
The strict type comparison (!==) is important there when using strpos/stripos.
The problem with strripos and its siblings is that they return the position of the substring found. So if the substring you're searching happens to be at the start, it returns 0 which in a boolean test is false.
Use:
if ( $pos !== FALSE ) ...
$variable = 'Pensioner (other)';
$pos = strripos($variable, 'pensioner');
if ($pos !== FALSE) {
echo 'found one';
} else {
echo 'not found';
}
^ Works for me. Note that strripos() is case insensitive. If you wanted it to be a case-sensitive search, use strrpos() instead.

Categories