Page 1 of 1
strpos problems
Posted: Fri Oct 13, 2006 4:15 am
by malcolmboston
php 4.4.0 on Win XP with error_reporting (E_ALL);
this function (within a class)
Code: Select all
function checkIsOurSite ($url) {
for ($i = 1; $i <= count ($this->relevantSites); $i++) {
output::outputString ('Looking for '.$this->relevantSites[$i].' in '.$url.' - ', '', '');
if (strpos($this->relevantSites[$i], $url)) {
print "FOUND<BR>";
} else {
print "NOT FOUND<BR>";
}
}
#return $found;
}
now the output
Code: Select all
Looking for redsquareclothing in http://redsquareclothing.com/ - NOT FOUND
it should be saying FOUND, whats the problem?
Posted: Fri Oct 13, 2006 4:17 am
by JayBird
You have your params the wrong way round in this line
Code: Select all
strpos($this->relevantSites[$i], $url)
Posted: Fri Oct 13, 2006 4:27 am
by malcolmboston
Code: Select all
function checkIsOurSite ($url) {
for ($i = 1; $i <= count ($this->relevantSites); $i++) {
output::outputString ('Looking for '.$this->relevantSites[$i].' in '.$url.' - ', '', '');
if (strpos($url, $this->relevantSites[$i])) {
print "FOUND<BR>";
} else {
print "NOT FOUND<BR>";
}
}
#return $found;
}
same output

Posted: Fri Oct 13, 2006 4:54 am
by malcolmboston
another method
Code: Select all
function checkIsOurSite ($url) {
for ($i = 1; $i <= count ($this->relevantSites); $i++) {
output::outputString ('Looking for '.$this->relevantSites[$i].' in '.$url.' - ', '', '');
if (strpos($url, $this->relevantSites[$i]) === FALSE) {
echo "not found<br>";
} else {
echo "found<br>";
}
}
#return $found;
}
Same output
Code: Select all
Looking for redsquareclothing in http://redsquareclothing.com/ - not found
Posted: Fri Oct 13, 2006 5:05 am
by Ollie Saunders
Arrays start from 0.
Code: Select all
for ($i = 1; $i <= count ($this->relevantSites); $i++) {
...should be...
Code: Select all
for ($i = 0; $i < count ($this->relevantSites); $i++) {
If you had error reporting on you would have had a nice big
Posted: Fri Oct 13, 2006 5:07 am
by JayBird
Solved...used
trim() on the values
Posted: Fri Oct 13, 2006 5:22 am
by Ollie Saunders
Well this works:
Code: Select all
class output
{
static public function outputString()
{
echo implode(func_get_args());
}
}
class Foo
{
public $relevantSites = array('http://redsquareclothing.com/');
public function checkIsOurSite ($url) {
for ($i = 0; $i < count ($this->relevantSites); $i++) {
output::outputString ('Looking for '.$this->relevantSites[$i].' in '.$url.' - ', '', '');
if (strpos($this->relevantSites[$i], $url) === FALSE) {
echo "not found<br>";
} else {
echo "found<br>";
}
}
#return $found;
}
}
$foo = new Foo;
$foo->checkIsOurSite('redsquareclothing');
Code: Select all
Looking for http://redsquareclothing.com/ in redsquareclothing - found
Posted: Fri Oct 13, 2006 8:15 am
by malcolmboston
ole wrote:If you had error reporting on you would have had a nice big
if you read my post you would of already known i do have error_reporting on....in fact it was the first line of my first post
php 4.4.0 on Win XP with error_reporting (E_ALL);
and yes, it was a trim problem not my code
edit:
it is my personal preference to start all arrays at 1