PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
malcolmboston
DevNet Resident
Posts: 1826 Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK
Post
by malcolmboston » Fri Oct 13, 2006 4:15 am
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?
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Fri Oct 13, 2006 4:17 am
You have your params the wrong way round in this line
Code: Select all
strpos($this->relevantSites[$i], $url)
malcolmboston
DevNet Resident
Posts: 1826 Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK
Post
by malcolmboston » Fri Oct 13, 2006 4:27 am
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
malcolmboston
DevNet Resident
Posts: 1826 Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK
Post
by malcolmboston » Fri Oct 13, 2006 4:54 am
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
Ollie Saunders
DevNet Master
Posts: 3179 Joined: Tue May 24, 2005 6:01 pm
Location: UK
Post
by Ollie Saunders » Fri Oct 13, 2006 5:05 am
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
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Fri Oct 13, 2006 5:07 am
Solved...used
trim() on the values
Ollie Saunders
DevNet Master
Posts: 3179 Joined: Tue May 24, 2005 6:01 pm
Location: UK
Post
by Ollie Saunders » Fri Oct 13, 2006 5:22 am
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
malcolmboston
DevNet Resident
Posts: 1826 Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK
Post
by malcolmboston » Fri Oct 13, 2006 8:15 am
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