Be a Supporter!

PHP: function strFind()

  • 8,864 Views
  • 4 Replies
New Topic Respond to this Topic
Pilot-Doofy
Pilot-Doofy
  • Member since: Sep. 13, 2003
  • Offline.
Forum Stats
Member
Level 37
Musician
PHP: function strFind() Jul. 3rd, 2006 @ 01:16 AM Reply

PHP: Main

If you've been around PHP for any amount of time I'm sure you're familiar with the predefined functions strstr() and stristr().

Well, you've probably also seen and/or used a few calls to the str_replace() function. As you probably realize, the str_replace() function has a MIXED variable type for all the argument of the function, yet it's still called str_replace().

MIXED variable types means you can pass different variable types
to the function and it still work, such as strings, arrays, etc.

So why can't strstr() and stristr() take mixed variable types? I was scratching my head and asking myself the same question. Well, wait no longer, here is a version of strstr() and stristr() combined which can also accept arrays OR strings are either argument. Here's the function:

function strFind($haystack, $needle, $case='i') {

$haystack = ( is_array($haystack) ) ? implode('', $haystack) : $haystack;

$func = ( $case == 'i' ) ? 'stristr' : 'strstr';

if ( is_array($needle) ) {
foreach($needle as $item) {
if ( $func($haystack, $item) ) {
return true;
}
}
} else {
if ( $func($haystack, $needle) ) {
return true;
}
}

return false; // Didn't find a match

} // End strFind()

Let me show you how to use it. Let's define our haystack and needle first.

$haystack = 'Hey guys!';
$needle = array('h', 'b', 'k');
$result = strFind($haystack, $needle);

In this case, the function would return true. Since by default the function searches with the case-insensitive method, it would match "h" and "H". However, this would return false:

$result = strFind($haystack, $needle, 's');

That tells it to search case-sensitively. While the actual value of the 3rd parameter doesn't matter (as long as it's not "i"), I just use "s" to make it clear what's going on.

Well, I hope you liked it.

whatthedeuce
whatthedeuce
  • Member since: Jun. 4, 2005
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to PHP: function strFind() Jul. 3rd, 2006 @ 02:57 AM Reply

With this function, the following succeeds:
strFind(array('hel', 'lo'), 'hello');

while this fails:
strFind(array('hel', 'ao', 'lo');

This probably isn't the behavior you want (it seems like the first one should fail).

Craige
Craige
  • Member since: Jul. 17, 2004
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to PHP: function strFind() Jul. 3rd, 2006 @ 01:26 PM Reply

At 7/3/06 02:57 AM, whatthedeuce wrote: With this function, the following succeeds:
strFind(array('hel', 'lo'), 'hello');

while this fails:
strFind(array('hel', 'ao', 'lo');

This probably isn't the behavior you want (it seems like the first one should fail).

Well, provided you finnish off that finction call, I would think they both should pass.

BTW Dan, it waild make more sence to use boolean values for the case sensitive paramiter. True for case sensitive, false for case in-sensitive.

whatthedeuce
whatthedeuce
  • Member since: Jun. 4, 2005
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to PHP: function strFind() Jul. 3rd, 2006 @ 04:54 PM Reply

At 7/3/06 01:26 PM, Craige wrote:
At 7/3/06 02:57 AM, whatthedeuce wrote: With this function, the following succeeds:
strFind(array('hel', 'lo'), 'hello');

while this fails:
strFind(array('hel', 'ao', 'lo');

This probably isn't the behavior you want (it seems like the first one should fail).
Well, provided you finnish off that finction call, I would think they both should pass.

BTW Dan, it waild make more sence to use boolean values for the case sensitive paramiter. True for case sensitive, false for case in-sensitive.

Sorry about the mistake in the code (that's what I get for posting code without trying it). It fails even if the code is correct.

Pilot-Doofy
Pilot-Doofy
  • Member since: Sep. 13, 2003
  • Offline.
Forum Stats
Member
Level 37
Musician
Response to PHP: function strFind() Jul. 4th, 2006 @ 06:44 PM Reply

At 7/3/06 01:26 PM, Craige wrote: BTW Dan, it waild make more sence to use boolean values for the case sensitive paramiter. True for case sensitive, false for case in-sensitive.

If I had actually written this function to go public I would have. I just wrote it for a guy over AIM and I actually wrote it in the AIM window. I just copy/pasted into the BBS since it was a decent add.