Page 1 of 1

Problem with a function :/

Posted: Thu Jul 08, 2010 12:42 pm
by dominod
Hi

I have a problem with this code. The problem is that it only redirect when I ONLY write "word" in the text field. I want it to redirect whenever "word" is submitted even if there is other characters in the textfield.

Is there any code that tells it that it will trigger whenever it finds that array in the text field?

Code: Select all

function wordsExist(&$string, $words) {
    foreach($words as &$word) {
        if(stripos($string, $word) !== false) {
            return true;
        }
    }
    return false;
}

if (wordsExist($search, array('word'))) {
    $redir = $search;
}

Thanks in advance :)

Re: Problem with a function :/

Posted: Thu Jul 08, 2010 12:59 pm
by AbraCadaver
I don't understand your problem. If the var $search contains word then these will both echo YES, just as they do with the strings below (I removed your two references as they are not needed):

Code: Select all

function wordsExist($string, $words) {
    foreach($words as $word) {
        if(stripos($string, $word) !== false) {
            return true;
        }
    }
    return false;
}

if (wordsExist('word', array('word'))) {
    echo "YES";
}

if (wordsExist('a word and more', array('word'))) {
    echo "YES";
}

Re: Problem with a function :/

Posted: Thu Jul 08, 2010 1:04 pm
by dominod
Thats what so strange about this .. Because it used to work fine untill I added a code to pull some information from a database and use it in a similar way:

Code: Select all

function wordExist(&$string, $string) {
        if(stripos($string, $string) !== false) {
            return true;
    }
    return false;
}

elseif (wordExist($search, $keyword))
{

	$redir =  $searchurl;
	}
For some reason it doesnt echo when it finds the code with other characters.. It only echos when it is ONLY the specific word.

Confused :banghead:

Re: Problem with a function :/

Posted: Thu Jul 08, 2010 1:18 pm
by Jade
You do realize you're passing the same variable in both parameters?

Re: Problem with a function :/

Posted: Thu Jul 08, 2010 1:49 pm
by dominod
do you mean &$string, $string ?

Is that a problem?

How can I else solve it?

But it is not that function who does not work .. it is the other (wordsExist)..

If I change those two variables, will the other function work then?

Re: Problem with a function :/

Posted: Thu Jul 08, 2010 2:03 pm
by dominod
Ok now I changed it to:

Code: Select all

function wordIsthere(&$string, $strin) {
        if(stripos($string, $strin) !== false) {
            return true;
    }
    return false;
}
But still no progress :/

Re: Problem with a function :/

Posted: Thu Jul 08, 2010 2:34 pm
by AbraCadaver
You're going to have to start showing all of your code, and STOP using the reference op &. You don't need it and it may be confusing you. This works, just as the example I posted earlier. What is the issue:

Code: Select all

function wordIsthere($string, $word) {
        if(stripos($string, $word) !== false) {
            return true;
    }
    return false;
}
if (wordExist('a word and something', 'word')) {
   echo "WORD IS THERE!";
}