This question already has answers here:
php 5 strpos() difference between returning 0 and false?
(5 answers)
If string contains forward slash
(4 answers)
Closed 5 years ago.
i try to check a string with strpos().
$a = $_SERVER['REQUEST_URI'];
This works just fine
if (strpos($a, 'en' ) == true)
But this doesn't
if (strpos($a, '/en/' ) == true) - Doesn't work
I tried a lot of things to escape the character or format the string but as it seems I am too stupid...
The issue is that strpos returns the position or FALSE if not found.
So if the url is /en/something/some then you are hitting the situation where en is a position 1 and any non-zero number is true
When you do /en/ then the starting position is 0 and that is false.
you need to check with === in the end or more accurately !== example
<?php
$a= "/en/blo";
if (strpos($a, '/en/' ) !== false){
echo "TRUE";
} else {
echo "FALSE";
}
Related
This question already has answers here:
Simple PHP strpos function not working, why?
(5 answers)
Closed 3 years ago.
I am trying to do a input check and to seeif the string contains " or '. I have the stringcheck method that looks like this
public static function stringcheck($search, $string)
{
$position = strpos($string, $search);
if ($position == true)
{
return true;
}
else{
return false;
}
}
And this is how i am trying to check it
if(H::stringcheck($search8, $string) === true || $string[0] === """)
{
$value++;
}
And the $search8 look like $search8 = htmlspecialchars('"');. I should also mention that the $string is already sanitized.
strpos returns the position of the match, which will be 0 if the match is at the beginning. But 0 is not == true.
I guess you are trying to work around that by extra checking $string[0] === """ which won't work, as I commented above: $string[0] is a single character, but " is 6 characters long.
This question already has answers here:
php 5 strpos() difference between returning 0 and false?
(5 answers)
Closed 7 years ago.
See the following issue:
$str = "video-23984"; // returns false
$str = " video-23984"; // returns true
$search= "video";
if(strpos($str,$search)) {
echo "True";
}else {
echo "False";
}
Why in the world does $str = "video-23984" return false? And what can I do to make it return true?
In the first string the word video is in the first position, which means 0.
In the second it's in the second position, which means 1.
Since you're returning it into an if, the 0 is a boolean for false. That's why you're getting false as a result.
This question already has an answer here:
symbol and decimal number true in php
(1 answer)
Closed 8 years ago.
I have script like this
if('#' == 0){
echo "true";
}else{
echo "false";
}
output :
true
the question is why it get true and how to get it false?
thanks.
As Mark Baker stated in the comments, PHP uses weak typing. According to the comparison matrix that you find here (http://www.php.net/manual/en/types.comparisons.php), the expression involving the string '#' compared to 0 will be evaluated as true.
Change your condition to:
if('#' === 0){
...
in this case you'll get false.
For this condition to get true of false properly, you need to compare the value and the type of the operators. Try this:
if('#' === 0){
echo "true";
}else{
echo "false";
}
You will get false for this case.
This question already has answers here:
startsWith() and endsWith() functions in PHP
(34 answers)
Closed 9 years ago.
How do one determine,if the strings appears at the end of the other string. If they do then print true to standard out and false if they don’t. Would strpos will help?
Sample Input
S1: “staff”
S2: “FF”
how would i make a function to run this,
function matching_ends($s1,$s2){
}
<?php
$s1="testing";
$s2="ing";
echo matching_ends($s1, $s2);
function matching_ends($s1,$s2){
return substr($s1, -strlen($s2)) == $s2 ? "true" : "false";
}
?>
Demo
You can use this
if( substr($s1, strlen($s1)-1) === substr($s2, strlen($s2)-1))
{
// Do something when character at the last matches
}
else{
// Do something when doesn't match
}
This question already has answers here:
Variable position in comparision in PHP
(4 answers)
Closed 9 years ago.
what is the difference between these two expressions ,
if(false == $flag)
if($flag == false)
what is the benefit of this expression rather than normal initiation like this :
if($flag == false)
In first case, if(false == $flag), compiler warn you if you type accidentally = instead of ==. But this is not true in the second case, if($flag == false).
In second case if == is accidentally replaced by = then compiler do not show you any warning or error instead $flag = false will assign false to flag and the condition $flag = false will always be false.