00:00
00:00
Newgrounds Background Image Theme

Patrick8008 just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

PHP: function strFind()

12,874 Views | 4 Replies
New Topic Respond to this Topic

PHP: function strFind() 2006-07-03 01:16:32


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.


Merkd.com - It Pays to Play

Earn real money by betting and investing; or, sponsor, challenge, compete,

recruit, communicate, network, earn money playing games, and much more.

Response to PHP: function strFind() 2006-07-03 02:57:44


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

Response to PHP: function strFind() 2006-07-03 13:26:55


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.

Response to PHP: function strFind() 2006-07-03 16:54:15


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.

Response to PHP: function strFind() 2006-07-04 18:44:43


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.


Merkd.com - It Pays to Play

Earn real money by betting and investing; or, sponsor, challenge, compete,

recruit, communicate, network, earn money playing games, and much more.