...

View Full Version : how to check if a string is present in a portion of a string



me2
02-16-2008, 10:46 AM
i looked this up and found strpos..

[code]
if($rights >= $page['rights'] && strpos("/images/", $url) != 0)
[code]

this statement always returns false no matter what.. i have tried to use the === operator but it doesnt seem to work..

I am trying to see if the page has enough rights to be displayed but i want to ignore all the pages in the images subfolder..

Thanks in advance for all your help

oesxyl
02-16-2008, 11:36 AM
i looked this up and found strpos..

[code]
if($rights >= $page['rights'] && strpos("/images/", $url) != 0)
[code]

this statement always returns false no matter what.. i have tried to use the === operator but it doesnt seem to work..

I am trying to see if the page has enough rights to be displayed but i want to ignore all the pages in the images subfolder..

Thanks in advance for all your help

what values you have in $rights and $page['rights'] ?

best regards

Inigoesdr
02-16-2008, 03:17 PM
i looked this up and found strpos..

[code]
if($rights >= $page['rights'] && strpos("/images/", $url) != 0)
[code]

this statement always returns false no matter what.. i have tried to use the === operator but it doesnt seem to work..

I am trying to see if the page has enough rights to be displayed but i want to ignore all the pages in the images subfolder..

Thanks in advance for all your help

strpos() (http://php.net/strpos) returns false if the string is not found, so if you're trying to make sure the needle is in the haystack you want something like this:

if($rights >= $page['rights'] && strpos("/images/", $url) !== false)

me2
02-16-2008, 10:18 PM
i tried putting that in but it doesnt seem to do what i want it to do..


if(0 >= 0 && strpos("/images/", "/images/authimage.php") !== false)
is how it turns out when i translate all the values a file is in the images folder and it returns false but
if(0 >= 0 && strpos("/images/", "/unit/minutes.php") !== false)
this time the statement also equals false.. is there a way that i can make the first one false but the second one true??

Inigoesdr
02-17-2008, 02:06 AM
i tried putting that in but it doesnt seem to do what i want it to do..


if(0 >= 0 && strpos("/images/", "/images/authimage.php") !== false)
is how it turns out when i translate all the values a file is in the images folder and it returns false but
if(0 >= 0 && strpos("/images/", "/unit/minutes.php") !== false)
this time the statement also equals false.. is there a way that i can make the first one false but the second one true??

Is this what you're looking for?

if(0 >= 0 && strpos("/images/", "/images/authimage.php") === false) // false

if(0 >= 0 && strpos("/images/", "/unit/minutes.php") === false) // true
If not be more descriptive with your desired result.

Iszak
02-17-2008, 06:06 AM
I suggest using substr



if (strstr($string, "string"))
{
// ITS THERE
}

Inigoesdr
02-17-2008, 06:17 AM
I suggest using substr
You mean strstr() (http://php.net/strstr), like in the code you posted. If you check out the strstr() manual page you'll see this note:

Note: If you only want to determine if a particular needle occurs within haystack , use the faster and less memory intensive function strpos() instead.

Ultragames
02-17-2008, 06:27 AM
Inigoesdr is correct, though I suggest using stripos() (http://us.php.net/manual/en/function.stripos.php) to avoid case issues, and remember to use the Identical operator as others mentioned.

Rohan_Shenoy
02-17-2008, 09:05 AM
Why don't you use PHP regular expressions? You can find a very useful tutorial at http://www.sitepoint.com/article/regular-expressions-php

Though learning regular expressions may take a slighty longer time, its is well worth it ;)

Inigoesdr
02-17-2008, 12:32 PM
There's no need to load the regex engine for something as trivial as this.

Tip

Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster.

me2
02-19-2008, 04:56 AM
Sorry about the late reply


Is this what you're looking for?

if(0 >= 0 && strpos("/images/", "/images/authimage.php") === false) // false

if(0 >= 0 && strpos("/images/", "/unit/minutes.php") === false) // true
If not be more descriptive with your desired result.

now they both return true

if(0 >= 0 && stripos("/images/", "/unit/minutes.php") === false) // i want that to return false

and for

if(0 >= 0 && stripos("/images/", "/images/authimage.php") // to return true..

thanks

oesxyl
02-19-2008, 05:22 AM
if($rights >= $page['rights'] && preg_match("/\/images\//",$url)){
}


best regards

me2
02-19-2008, 05:48 AM
thank you for your help now it does work and if anyone else can come up with a solution that doesn't have to load regex i will be happier..

Inigoesdr
02-19-2008, 06:09 AM
if anyone else can come up with a solution that doesn't have to load regex i will be happier..

You've already been shown two solutions that don't involve loading the regex engine.

oesxyl
02-19-2008, 06:54 AM
You've already been shown two solutions that don't involve loading the regex engine.

this strpos("/images/", "/images/authimage.php") return 0, because it find '/images/' to position 0, php assume is false.

me2 is right, both return true.

best regards

Inigoesdr
02-19-2008, 08:31 AM
this strpos("/images/", "/images/authimage.php") return 0, because it find '/images/' to position 0, php assume is false.

me2 is right, both return true.

best regards
Right, because me2 didn't copy the code I posted, or use an equivalent. PHP will only consider it false if you're doing an "equal" comparison, not an "identical"(type) comparison.

var_dump(0 == false); // true
var_dump(0 === false); // false
If you wanted to compare for NOT identical you would use:

var_dump(0 !== false); // true
See more in the manual (http://php.net/language.operators.comparison).

oesxyl
02-19-2008, 12:44 PM
I know the manual stuff, I was convinced as you are that me2 is wrong, but when he said that both if's are true, I tested, and I write myself another test.
Maybe I do something wrong, check your self:



<?php
$tocheck = "/images/";
$first = "/images/authimage.php";
$second = "/unit/minutes.php";

if(strpos($tocheck,$first) === false){
print $first." contain ".$tocheck."<br>";
}else{
print $first." NOT contain ".$tocheck."<br>";
}

if(strpos($tocheck,$second) === false){
print $second." contain ".$tocheck."<br>";
}else{
print $second." NOT contain ".$tocheck."<br>";
}

?>


As you can see I use '==='.
I use PHP Version 5.2.0-8 and if you need I can post more info about what I have installed.
Obvious is that I make something wrong or is a bug somewhere.

best regards

Scrumpy.Gums
02-19-2008, 12:58 PM
RTFM :thumbsup: ...you got the arguments the wrong way around and quite obviously $first is not contained within $tocheck :rolleyes:


<?php
$tocheck = "/images/";
$first = "/images/authimage.php";
$second = "/unit/minutes.php";

if(strpos($first, $tocheck) !== false)
echo "true<br/>";
else
echo "false<br/>";

if(strpos($second, $tocheck) !== false)
echo "true<br/>";
else
echo "false<br/>";

?>

returns:

true
false

Which is what is expected

oesxyl
02-19-2008, 02:16 PM
RTFM :thumbsup:
Do you realy know what RTFM means? or you just try to be "cool"?
In first case you can control your self, it's not so hard, just try.


...you got the arguments the wrong way around and quite obviously $first is not contained within $tocheck :rolleyes:
[/CODE]

Which is what is expected
yes you are right, :)

best regards

oesxyl
02-19-2008, 02:40 PM
what's wrong with this code?



<?php
if(0 >= 0 && strpos("/images/", "/images/authimage.php") === false){ // false
print "this line is not ok<br>";
}

if(0 >= 0 && strpos("/images/", "/unit/minutes.php") === false){ // true
print "this line is ok<br>";
}

if(strpos("/images/", "/images/authimage.php") === false){ // false
print "this line is not ok<br>";
}

if(strpos("/images/", "/unit/minutes.php") === false){ // true
print "this line is ok<br>";
}
?>


this is copy and paste from your post Inigoesdr, I only add print lines.
I don't see anything wrong in logic, only the results. This was first test, before that from my previous post.
It must output 2 lines, correct me if I'm wrong, but display 4:



this line is not ok
this line is ok
this line is not ok
this line is ok


best regards

oesxyl
02-19-2008, 03:06 PM
Ok, I discover, :). Thanks to Scrumpy.Gums, :)
same mistake, all of as, order of arguments in strpos, :)



int stripos ( string $haystack , string $needle [, int $offset ] )

Returns the numeric position of the first occurrence of needle in the haystack string.




<?php

if(0 >= 0 && strpos("/images/authimage.php", "/images/") === false){ // false
print "this line is not ok<br>";
}

if(0 >= 0 && strpos("/unit/minutes.php", "/images/") === false){ // true
print "this line is ok<br>";
}

if(strpos("/images/authimage.php", "/images/") === false){ // false
print "this line is not ok<br>";
}

if(strpos("/unit/minutes.php", "/images/") === false){ // true
print "this line is ok<br>";
}
?>


this is strpos version, me2, :)

best regards

me2
02-20-2008, 03:50 AM
wow thanks guys.. that finally works the way i wanted.. didn't know it was that trivial



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum