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'
Related
Can anyone tell me why this isn't working? It always returns false.
$str = "huuhhu\r\n\r\nmoo.com\r\nwww";
if (preg_match('/(\\n|\\r\\n|\\r)/', $str) === true) {
echo "True";
} else {
echo "False";
}
preg_match doesn't return true. It returns the number of matches. You need to do this:
$str = "huuhhu\r\n\r\nmoo.com\r\nwww";
if (preg_match('/(\\n|\\r\\n|\\r)/', $str)) {
echo "True";
} else {
echo "False";
}
Also, you could probably simplify your expression to this:
'/\n|\r\n?/'
preg_match() returns the number of times pattern matches and FALSE if an error occurred. It never returns true.
$filename = 'my_upgrade(1).zip';
$match = 'my_upgrade';
if(!strpos($filename, $match))
{
die();
}
else
{
//proceed
}
In the code above, I'm trying to die out of the script when the filename does not contain the text string "my_upgrade". However, in the example given, it should not die since "my_upgrade(1).zip" contains the string "my_upgrade".
What am I missing?
strpos returns false if the string is not found, and 0 if it is found at the beginning. Use the identity operator to distinguish the two:
if (strpos($filename, $match) === false) {
By the way, this fact is documented with a red background and an exclamation mark in the official documentation.
The strpos() function is case-sensitive.
if(strpos($filename, $match) !== false)
{
// $match is present in $filename
}
else
{
// $match is not present in $filename
}
For using case-insensitive.
use stripos() that is it finds the position of the first occurrence of a string inside another string (case-insensitive)
if (strpos($filename, $match) === false)
Otherwise, strpos will return 0 (the index of the match), which is false.
The === operator will also compare type of the variables (boolean != integer)
false === strpos($filename, $match)
The strpos functionDocs returns false if not found or 0 if found on position 0 (programmers like to start counting at 0 often):
Warning 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.
strpos returns false if the string is not found, and 0 if it is found at the beginning.
so try this:
if (strpos($filename, $match) === false) {
conditon
}
Note we use a strict comparison (===) If we don't, both types (false and false or an integer) will be coerced to the same type, and 0 when coerced to a bool is false.
more details: https://zdevtips.com/guid/strpos-in-php-example4-example-codes/
This working for me when everything other fail in some situations:
$filename = 'my_upgrade(1).zip';
$match = 'my_upgrade';
$checker == false;
if(strpos($filename, $match))
{
$checker == true;
}
if ($checker === false)
{
die();
}
else
{
//proceed
}
Or in short:
$filename = 'my_upgrade(1).zip';
$match = 'my_upgrade';
$checker == false;
if(strpos($filename, $match))
{
$checker == true;
//proceed
}
if ($checker === false)
{
die();
}
$data = 'match';
$line = 'match word in this line';
if(strpos($line,$data) !== false){
echo "data found";
}else{
echo "no data found";
}
strpos in this case will return a zero, which is then interpretted as false when you do the logical negation. You should check explicitly for the boolean false.
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.
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.
I have a string and it can contain values like this
$string = '1,2,3,4,5';
I wanna put a check that will see if the string contains 4 or 5
if it contains 4 or 5 then I want to echo success
otherwise if it contains 9 or 10 I wanna echo fail
I know there is a n in_array function but not sure how to use it
thanks
You can test for the number 4 like this:
if(in_array('4', explode(',', $string))) echo "it's in there";
or just by string searching:
if(strpos(',4,', ','.$string.',') !== false) echo "it's in there";
in_array won't help you here because you have a string, not an array. What you're looking for is the strpos() function:
http://php.net/manual/en/function.strpos.php
Note that if it doesn't find what it's looking for in your string, it'll return false on its own, so all you have to do is check whether it returns a result or not to meet your conditions.
$set = array (1,2,3...,n); //you can use range() function if the numbers going one by one or explode if you have a string
if(in_array($var,$set))
{
Echo 'IS in ARRAY!';
} else {
Echo 'fail';
}
$goodString = '1,2,3,4,5';
$badString = '1,2,3,7,8,9,10';
function checkString($str) {
$arr = explode(',', $str);
$message = 'no message';
if (
in_array(4, $arr)||
in_array(5, $arr)
) {
$message = 'success';
} else if (
in_array(9, $arr)||
in_array(10, $arr)
) {
$message = 'fail';
}
echo $message;
}
checkString($goodString); // prints success
checkString($badString); // prints fail
you can use strpos() to check for the existence of a substring inside a string, like this:
if(strpos(','.$string.',', ','.$number_to_check_for.',') !== false) {
//success, substring was found
} else {
//error, substring was not found.
}
or you could explode it into an array then use in_array():
$array = explode(',',$string);
if(in_array($number_to_check_for, $array)) {
//success substring found
} else {
//error, substring not found
}
But I would recommend the first solution, as it is cleaner and more efficient.
Check all at once :)
if(in_array(array(4,5), explode(',', $string))) echo "success";
if(in_array(array(9,10), explode(',', $string))) echo "failure";