Do something if variable matches pattern "title=*" - php

if($_SERVER['QUERY_STRING'] == "title=*") {echo 'do something!';}
This code does not function as I had hoped. Is there a simple method to:
if($_SERVER['QUERY_STRING'] == "title=/*ANYTHING*/") {echo 'do something!';}

Read this: http://php.net/manual/en/function.strpos.php
$pos = strpos($_SERVER['QUERY_STRING'], 'title=');
if ($pos === 0) {
// do something
}
strpos returns false if it can't find the string, otherwise the position it found it in.
In your case, that position needs to be 0 (note the ===)

Related

PHP's strpos() not working and always getting into the if condition

I'm trying to check if a specific character is not present in a string in my code and apparently php doesn't care about anything and always gets inside the if
foreach($inserted as $letter)
{
if(strpos($word, $letter) !== true) //if $letter not in $word
{
echo "$word , $letter, ";
$lives--;
}
}
In this case $word is "abc" and $letter is "b", I've tried changing a lot of random things like from true to false and things like that but I can't get it, can anyone help me please?
Changing the way you validate should fix it, like below:
foreach($inserted as $letter)
{
//strpos returns false if the needle wasn't found
if(strpos($word, $letter) === false)
{
echo "$word , $letter, ";
$lives--;
}
}
if(strpos($word, $letter) === false) //if $letter not in $word
{
echo "$word , $letter, ";
$lives--;
}
also, be careful to check explicitly against false, strpos can return 0 (a falsey value) if the match is in the 0th index of the string...
for example
if (!strpos('word', 'w') {
echo 'w is not in word';
}
would output the, possibly confusing, message 'w is not in word'

Check if string starts with http or www or /

This works:
if (strpos($page, 'http') == 0) {
$link = 'Test';
}
But this doesn't:
if (strpos($page, 'http' || 'www' || '/') == 0) {
$link = 'Test';
}
I need to return something if $page does not begin with any of those three: http, www, or /.
if (strpos($page, 'http') == 0 || strpos($page, 'www') == 0 || strpos($page, '/') == 0) {
$link = 'Test';
}
you cannot use || like that.
Other than the 'bad arguments' answers above, you've also got a serious logic bug. strpos can and WILL return a boolean FALSE if the 'needle' string isn't found in the 'haystack'. e.g.
$needle = 'foo';
$haystack = 'bar';
if (strpos($haystack, $needle) == 0) {
echo 'found it!';
}
will say found it!, because strpos returned boolean FALSE, which PHP then typecast over to an int to compare to the 0. (int)FALSE becomes 0.
You need use the strict comparison operator === to make sure you really are comparing int to int, and not int to possibly-boolean-false:
if (strpos(...) === 0) { ... }
Unfortunately PHP doesn't understand "If the door is red or green or blue". You have to spoon-feed it "if the door is red or the door is green or the door is blue". But there's still some shortcuts you can take:
if( preg_match("(^(?:http|www|/))",$page)) $link = "Test";
I recommend using regular expressions for a case like this when you need to do pattern matching. More efficient in every way and way easier once you get the gist of it. This is a very helpful guide: http://oreilly.com/catalog/regex/chapter/ch04.html

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

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

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