Forum Moderators: coopster

Message Too Old, No Replies

PHP syntax - find any one of multiple "needles" in a "haystack"

         

hermes

12:33 pm on Apr 28, 2005 (gmt 0)

10+ Year Member



I am familiar with the stristr function
[uk2.php.net...]
string stristr ( string haystack, string needle )
For example:

if (stristr('haystack', 'needle')

This returns TRUE if 'needle' is found in the haystack string.

Q1)

How would I go about writing up this pseudocode

if (stristr('haystack', 'needle1' or 'needle2' or 'needle3')

That is - I want it to return TRUE if either 'needle1', 'needle2' or 'needle3' are
found in the haystack string. If none of them are found - it returns FALSE.
IS there any better way than:

if ((stristr($haystack, 'needle1') or (stristr($haystack, 'needle2') or (stristr($haystack, 'needle3'))

I am especially worried because I may well be searching the same haystack for a lot of needles. And the above approach seems a bit messy to me for a lot of needles. There must be a more elegant way.

Q2)

Consider this code. Note the! - the!stristr

if (stristr('haystack', 'needle') &&!stristr('haystack', 'needle2')

Is there anyway to make this code smaller? - faster? - nicer? Given that both elements
are dealing with the same haystack.

Q3)

I take it that these two lines are equivalent

if ($variable!== 23 or 35 or 67 or 12)
if ($variable!== 23 ¦ 35 ¦ 67 ¦ 12)

Is there any nicer, cleaner, faster way of doing this. Could I perhaps put all the
elements in an array - and then somehow use this array against the $variable element. WOuld
this approach be at all faster? How would I go about this? There are not many numbers in the lines above - but in my actual code I could actually be using a lot of numbers.

I realise that this stuff is pretty basic. But would be v.grateful for any help.

hermes

1:00 pm on Apr 28, 2005 (gmt 0)

10+ Year Member



Am I going anywhere with this pseudocode?

array = needle1, needle2, needle3, needle4

(stristr('haystack', 'any needle in the array of needles')

patrickrock

2:17 pm on Apr 28, 2005 (gmt 0)

10+ Year Member



you cannot pass an array to stristr().

Detail your problem a little more, there may be a way with regular expressions, but I need a clearer picture of what you want to do.

hermes

2:28 pm on Apr 28, 2005 (gmt 0)

10+ Year Member



maybe forget about the array issue - it was just a stab by me as an avenue for a solution. A stab by someone that doesnt really no much about php. The salient issue really is:

if (stristr('haystack', 'needle1' or 'needle2' or 'needle3')

That is - I want it to return TRUE if either 'needle1', 'needle2' or 'needle3' are
found in the haystack string. If none of them are found - it returns FALSE.
IS there any better way than:

if ((stristr($haystack, 'needle1') or (stristr($haystack, 'needle2') or (stristr($haystack, 'needle3'))

Because if I use this method - with the number of needles I will be using - will be very long and messy. Is there anyway that I can do this "cleaner".

If anyone cant figure out what I mean - please do post and I will try to clarify more. I may not have explained the isse very clearly.

patrickrock

3:19 pm on Apr 28, 2005 (gmt 0)

10+ Year Member



Ah. Ok, here is one way to do it. You want your friend the foreach loop:

$array_needles = array("needle1", "needle2", etc...);
$array_found_needles = array();

$haystack = "haystack";

foreach($array as $key=>$val){
if(stristr($haystack, $val){
//do whatever you want if its found
echo "$val was found"
}
}


how about that?

FourDegreez

9:01 pm on Apr 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think hermes is looking to avoid looping, possibly for performance considerations. Sorry, I don't have a solution though. Rather new to PHP myself.

patrickrock

3:13 pm on Apr 29, 2005 (gmt 0)

10+ Year Member



I can't think of any way to do what he wants without using a loop, or that really long string of ¦¦ statements that he wants to avoid.

A case statement that is as long as there are needles to look for?

Stormfx

8:56 pm on Apr 29, 2005 (gmt 0)

10+ Year Member



Write a function:

function mstri($hay, $ndla, $ndlb, $ndlc){

if (stristr($hay, $ndla) ¦¦ stristr($hay, $ndlb) ¦¦ stristr($hay, $ndlb)) {
return true;
}

return false;
}

hermes

2:32 pm on May 1, 2005 (gmt 0)

10+ Year Member



will try the avenues listed here. Thanks guys. Speed is a major concern for me. So, is the consensus that the function route will be the fastest?

ON a second query:

if ($variable!== 0 ¦ -60 ¦ -120 ¦ 480 ¦ 420 ¦ 360 ¦ 300 ¦ 240 ¦ 180)

so if the $variable does NOT have the value 0 or -60 or -120...... return true.

BUT I cannot get the above line to work. What is wrong with it?

Stormfx

5:50 pm on May 1, 2005 (gmt 0)

10+ Year Member



The function method would require less lines of code if you're using the comparison multiple times. But reguardless of how you code it, it'll take about the same amount of time and the same amount of overhead.

As far as this post above goes, you cannot just specify each item, you have to explicitely redine the expression for each comparison number.

So this statement:

if ($variable!== 0 ¦ -60 ¦ -120 ¦ 480 ¦ 420 ¦ 360 ¦ 300 ¦ 240 ¦ 180)

Is saying: If $variable is NOT equal in both value and type to the integer 0 OR -60 evaluates to true, OR -120 evaluates to true, OR... etc etc..

When it should be:

if ($variable!== 0 ¦ $variable!== -60 ¦ $variable!== -120 ¦ $variable!== 480 ¦ $variable!== 420 ¦ $variable!== 360 ¦ $variable!== 300 ¦ $variable!== 240 ¦ $variable!== 180)

Which says: If $variable is NOT equal in both value and type to the integer 0 OR $variable is NOT equal in both value and type to the integer -60 OR $variable is NOT equal in both value and type to the integer -120 OR etc, etc....

See? Every comparison has to be defined by the complete expression seperately. For a situation like this, you'd use a switch statement:

switch ($variable) {
case 0:
//do something
break;
case -60:
//do something
break;
case -120:
//do something
break;
case 480:
//do something
break;
case 420:
//do something
break;
case 360:
//do something
break;
case 300:
//do something
break;
case 240:
//do something
break;
case 180:
//do something
break;
default:
// Do some default method..
break;
}