note 47322 deleted from function.strpos by nlopess
| From: | nlopess@php.net | Date: | Tue, 25 Jul 2006 20:15:38 +0000 |
| Subject: | note 47322 deleted from function.strpos by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-115191@lists.php.net to get a copy of this message | ||
Note Submitter: James Perlman
----
Here's a nifty function i made, that allows you to search a string using wildcards. It was
intentionally made as part of a ban system.
Here is the function:
(below is how to use the function)
<?php
function findStr($search, $target) {
$matches = 0;
$search = strtolower($search);
$target = strtolower($target);
$output = "";
// Create the "search" array, which holds all our search terms
$search = explode("*",$search); // You could change this to: '$search =
explode(" ",$search);' if you wanted your search terms to be split by a space.
$pos = 0;
for ($i=0; $i<count($search); $i++) {
// Check if the current search term is in our target
if (strpos($target, $search[$i], $pos) != '' && strlen($search[$i])>0) {
$pos = strpos($target, $search[$i], $pos);
$matches++;
}
if (strlen($search[$i])<1) {
$matches++;
}
}
if ($matches == count($search)) {
return true;
} else {
return false;
}
}
?>
How to use the function:
<?
echo findStr("34.12.85.*","34.12.85.98"); // would return true
echo findStr("3*.12.*.90","39.16.29.34"); // would return false
?>
The function searches to see if all the search terms are in the string, and if they are in the exact
order you gave them in.
Hope you found this function to be helpful!
-James