Check if string starts with http or www or / - php

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

Related

Make the function return 0 and store it in a variable

Seems super trivial, but can't find a solution to this specific case on SO
A function may return a value of 0 OR another number, which I then want to store in a variable $s to calculate stuff. But can't find a clean way of doing it.
So for example:
function f() {
$v = "0";
return $v;
}
if($s = f() !== false) {
echo $s;
// ^ I want 0, but the value above is 1 (since it's true)
}
I tried returning it as a string
return "0" instead of a digit, but it doesn't work.
If I do this it will not evaluate to true so nothing will happen
if($s = f()) {
// returns nothing
}
But when I var_dump(f()), it does show string '0' (length=1)
So I can do
if(var_dump(f()) == 0)
OR
if(f() == 0)
But is there not a cleaner way to do it, so the function may return 0 or another number and I can just capture it in a variable?
Add parentheses:
if (($s = f()) !== false) {
Otherwise you're computing the value f() !== false and assigning it to $s, instead of assigning f() to $s and then comparing to false.
First
if($s = f() !== false) {
echo $s;
// ^ I want 0, but the value above is 1 (since it's true)
}
Whats happening here is return value of f() is strictly compared to false, that is, "0" is compared to false. They are not strictly equal, hence f() !== false returns true, which is stored in $s as 1.
Next,
if($s = f()) {
// returns nothing
}
This doesnt enter the if block because $s contains "0" and it is loosely equal to false.
Next
if(var_dump(f()) == 0) or if(f() == 0)
this works because it is loosely comparing "0" to 0 which is true. Hence it enters the block.
Remember, if condition does loose comparision or == and === does strict comparision. More about it here.
So, this should work in your case
if(($s = f()) !== 0)
if f() returns integers
Try some parenthesis:
if(($s = f()) !== false) {
To force $s to equal f(), not f() !== false

Do something if variable matches pattern "title=*"

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 ===)

Case insensitive function arguments

Is there a way to make php function arguments case insensitive? Right now I am looking for the word "Crossing" however "crossing" does not match. How can I make this argument case insensitive so I don't have to || each type of casing it could be put in as (if possible)
Function:
function endsWith($haystack, $needle) {
return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== false);
}
PHP:
if(endsWith($searchResults, "Crossing")) {
$searchResults = str_replace("Crossing", "Xing", $searchResults);
}
This was resolved using the following:
if(stripos($searchResults, "Crossing")) {
$searchResults = str_ireplace("Crossing", "Xing", $searchResults);
}
You're looking for strnatcasecmp()
if( strnatcasecmp($varA, $varB) )
Ref: http://php.net/manual/en/function.strnatcasecmp.php
You can also just use strtolower() or strtoupper() for comparisons. Example:
if( 'test' == strtolower($variable) )
I personally just use the later in general as long as I know what is expected, but for comparison of two true variables where case is non-sensitive strnatcasecmp() is the way to go.

strpos with two words to find

I found this example on stackoverflow:
if (strpos($a,'are') !== false) {
echo 'true';
}
But how do I make it search for two words. I need something like this: if $a contains the words "are" or "be" or both echo "contains";
I tried xor and ||
Just check both words separately and use the boolean or-operator to check if either one or both are contained in $a:
if (strpos($a,'are') !== false || strpos($a,'be') !== false) {
echo "contains";
}
Note that due to the or-operator, the second check (for 'be') is not performed if the first one already showed that $a contains 'are'.
An alternative: Searches any length of words in the longer string.
Since you've haven't picked an answer from all the strpos answers (most of which should work with just two words, try this function of mine which exceeds word limits. It can find any varying length of words from the longer string (but doesn't use strpos). I think with strpos, you would have to know the number of words to determine how many || you should use or make use of a loop (sort of). This method eliminates all that and gives you a more flexible way to reuse your codes. I thinks codes should be flexible, reusable and dynamic. Test it and see if it does what you want!
function findwords($words, $search) {
$words_array = explode(" ", trim($words));
//$word_length = count($words_array);
$search_array = explode(" ", $search);
$search_length = count($search_array);
$mix_array = array_intersect($words_array, $search_array);
$mix_length = count($mix_array);
if ($mix_length == $search_length) {
return true;
} else {
return false;
}
}
//Usage and Examples
$words = "This is a long string";
$search = "is a";
findwords($words, $search);
// $search = "is a"; // returns true
// $search = "is long at"; // returns false
// $search = "long"; // returns true
// $search = "longer"; // returns false
// $search = "is long a"; // returns true
// $search = "this string"; // returns false - case sensitive
// $search = "This string"; // returns true - case sensitive
// $search = "This is a long string"; // returns true
$a = 'how are be';
if (strpos($a,'are') !== false || strpos($a,'be') !== false) {
echo 'contains';
}
Try:
if (strpos($a,'are') !== false || strpos($a,'be') !== false)
echo 'what you want';
if ((strpos($a,'are') !== false) || (strpos($a, 'be') !==false) {
echo 'contains';
}
Is this what you want?
if ((strpos($a,'are') !== false) || (strpos($a,'be') !== false)) {
echo 'contains';
}
if (strpos($a,'are') !== false || strpost($a, 'be') !== false) {
echo "contains";
}
Brain Candy:
If the first one returns true, it'll skip the second check. So both can be true. If the first one is false, ONLY then will it check the second. This is called a short circuit.
if(strstr($a,'are') || strstr($a,'be')) echo 'contains';
Hum, like this?
if (strpos($a,'are') || strpos($a, 'be') {
echo 'contains';
}

Strange result when combining assignment and equality in if statement (PHP)

Tested in PHP 5.3.10 on CentOS.
In a script a run:
$test = "62 3/4";
if($pos = strpos($test,' ') !== false) {
$test= substr($test,0,$pos); // use $pos
}
// $test is "6"
And in another independent script I run:
if($pos = strpos($test,' ') !== false) {
$test = substr($test,0,strpos($test,' ')); // redo substr calculation
}
// $test is "62"
$pos should be 2 (the third character is a space, starting from zero, 0,1,2), so both $test should be "62", no?
Operator precedence! !== comes before =, so the test effectively becomes
if($pos = (strpos($test,' ') !== false))
which is going to evaluate to either true or false, not the string position.
Always use explicit parens:
if(($pos = strpos($test,' ') !== false)
You want
if( ($pos = substr($test, ' ')) !== false ) {
// ...
}
See PHP Operator Precedence
Also consider that in your second if, the value of the variable $test was already changed by the first: $test= substr($test,0,$pos);

Categories