Jump to content

Problem with a function :/


dominod

Recommended Posts

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?

 

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 :)

Link to comment
Share on other sites

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):

 

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";
}

Link to comment
Share on other sites

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:

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

Link to comment
Share on other sites

No I am using wordExist and wordsExist (with an s)

 

And for the needle and the haystack .. I cant use &$string, $string ?

 

Sorry I am a newbie and this isnt logical for me yet.. What can I put there instead of &$string, $string ?

Link to comment
Share on other sites

No I am using wordExist and wordsExist (with an s)

 

And for the needle and the haystack .. I cant use &$string, $string ?

 

Sorry I am a newbie and this isnt logical for me yet.. What can I put there instead of &$string, $string ?

 

Uhh, $string and $words like in your first example.  Consider this:

 

function test($string, $string) {
  echo $string;  // what is $string?  a or b?
}

test('a', 'b');

Link to comment
Share on other sites

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

 

Ok got it .. I changed it to the code above now .. But it still wont redirect other when I type in only the exact word..

Link to comment
Share on other sites

Can you clarify that? Maybe provide some examples of parameters you'd pass to the function and the expected results of each?

 

Hi

 

I have a problem with this code. The problem is that it only redirect when I ONLY write "word" in the text field.

 

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 :)

Link to comment
Share on other sites

I tried what u said with no luck  :shrug:

 

Here is the entire code if you want to have a look:

 

<?php
ini_set("display_errors", true);
error_reporting(-1);

include("connect.php");
$extract = mysql_query("SELECT * FROM engines WHERE searchurl LIKE '%$search%'");
$numrows = mysql_num_rows($extract);
if($numrows == '0') {
$extract = mysql_query("SELECT * FROM engines WHERE keyword LIKE '%$search%'");
}

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

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

while ($row = mysql_fetch_array($extract))
{
$id=$row['id'];
$name=$row['name'];
$url=$row['url'];
$searchurl=$row['searchurl'];
$keyword=$row['keyword'];


$keyword = preg_replace('/([a-zA-Z0-9]*(\s){0,1}):/','',$search);
$shop = preg_replace('/:([a-zA-Z0-9]*(\s){0,1})/','',$search);




if (wordsExist($search, array('http://'))) {
    $redir = $search;
}

elseif (wordsExist($search, array('.com','.net','.org','.aero','.asia','.biz','.cat','.coop','.edu','.gov','.info','.int','.jobs','.mil','.mobi','.museum','.name','.pro','.tel','.travel','.ac','.ad','.ae','.af','.ag','.ai','.al','.am','.an','.ao','.aq','.ar','.as','.at','.au','.aw','.ax','.az','.ba','.bb','.bd','.be','.bf','.bg','.bh','.bi','.bj','.bm','.bn','.bo','.br','.bs','.bt','.bv','.bw','.by','.bz','.ca','.cc','.cd','.cf','.cg','.ch','.ci','.ck','.cl','.cm','.cn','.co','.cr','.cu','.cv','.cx','.cy','.cz','.de','.dj','.dk','.dm','.do','.dz','.ec','.ee','.eg','.er','.es','.et','.eu','.fi','.fj','.fk','.fm','.fo','.fr','.ga','.gb','.gd','.ge','.gf','.gg','.gh','.gi','.gl','.gm','.gn','.gp','.gq','.gr','.gs','.gt','.gu','.gw','.gy','.hk','.hm','.hn','.hr','.ht','.hu','.id','.ie','.il','.im','.in','.io','.iq','.ir','.is','.it','.je','.jm','.jo','.jp','.ke','.kg','.kh','.ki','.km','.kn','.kp','.kr','.kw','.ky','.kz','.la','.lb','.lc','.li','.lk','.lr','.ls','.lt','.lu','.lv','.ly','.ma','.mc','.md','.me','.mg','.mh','.mk','.ml','.mm','.mn','.mo','.mp','.mq','.mr','.ms','.mt','.mu','.mv','.mw','.mx','.my','.mz','.na','.nc','.ne','.nf','.ng','.ni','.nl','.no','.np','.nr','.nu','.nz','.om','.pa','.pe','.pf','.pg','.ph','.pk','.pl','.pm','.pn','.pr','.ps','.pt','.pw','.py','.qa','.re','.ro','.rs','.ru','.rw','.sa','.sb','.sc','.sd','.se','.sg','.sh','.si','.sj','.sk','.sl','.sm','.sn','.so','.sr','.st','.su','.sv','.sy','.sz','.tc','.td','.tf','.tg','.th','.tj','.tk','.tl','.tm','.tn','.to','.tp','.tr','.tt','.tv','.tw','.tz','.ua','.ug','.uk','.us','.uy','.uz','.va','.vc','.ve','.vg','.vi','.vn','.vu','.wf','.ws','.ye','.yt','.za','.zm','.zw')))
{
$redir = "http://$search";
}



elseif (wordsExist($search, array('tlf:')))
{

$redir = "http://opplysning1890.no/index.php?q=$keyword&type=privat"; //this should be a URL!
}

elseif (wordsExist($search, array(':')))
{
echo "There is no search engine connected with <b>$shop</b>. If you want to suggest a search engine please <a href=\"suggest.php\">click here</a>. If you rather want to search in Google for <b>$keyword</b>, please  <a href=\"http://www.google.com/search?q=$keyword\">click here</a>.";
}

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

$redir =  $searchurl;
}

else
{
    $redir = "http://www.google.com/search?q=$search";
}

}


if(isset($redir)) {
header("Location: $redir");
} else {
$redir = "http://www.google.com/search?q=$search";
header("Location: $redir");
}
?>

 

Link to comment
Share on other sites

This works as intended. I've tested it.

 

<?php
function wordsExist($string, $words) {
foreach($words as $word) {
	if( stripos($string, $word) !== false ) {
		return true;
	}
}
return false;
}
if (wordsExist('word', array('word'))) {
echo "word YES<br />";
}
if (wordsExist('a word and more', array('word'))) {
echo "a word and more YES<br />";
}
if (wordsExist('sword', array('word'))) {
echo "sword YES<br />";
}
if (wordsExist('swords', array('word'))) {
echo "swords YES<br />";
}
if (wordsExist('this is a haystack that doesn\'t contain the needle', array('word'))) {
echo "no needle YES<br />";
}
?>
/* 
RETURNS:
word YES
a word and more YES
sword YES
swords YES
*/

Link to comment
Share on other sites

Call me blind, but where does $search get a value? I don't see where $search is assigned a value anywhere in that script. The only way $search could have a value is if it relies on register_globals = On, or it's include()d in some other script.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.