Problem with a function :/

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
dominod
Forum Commoner
Posts: 75
Joined: Wed Jun 30, 2010 7:18 am

Problem with a function :/

Post 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 :)
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Problem with a function :/

Post 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";
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
dominod
Forum Commoner
Posts: 75
Joined: Wed Jun 30, 2010 7:18 am

Re: Problem with a function :/

Post 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:
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Problem with a function :/

Post by Jade »

You do realize you're passing the same variable in both parameters?
dominod
Forum Commoner
Posts: 75
Joined: Wed Jun 30, 2010 7:18 am

Re: Problem with a function :/

Post 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?
dominod
Forum Commoner
Posts: 75
Joined: Wed Jun 30, 2010 7:18 am

Re: Problem with a function :/

Post 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 :/
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Problem with a function :/

Post 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!";
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply