strpos problems

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

Post Reply
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

strpos problems

Post 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?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

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 »

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 :cry:
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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

Code: Select all

Notice: Undefined offset: 1
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Solved...used trim() on the values
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

ole wrote:If you had error reporting on you would have had a nice big

Code: Select all

Notice: Undefined offset: 1
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);
:roll:

and yes, it was a trim problem not my code

edit:

it is my personal preference to start all arrays at 1
Post Reply