note 47322 added to function.strpos
| From: | JamesPerlman at osu1 dot php dot net | Date: | Thu, 11 Nov 2004 08:06:24 +0000 |
| Subject: | note 47322 added to function.strpos | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-80397@lists.php.net to get a copy of this message | ||
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
----
Manual Page -- http://www.php.net/manual/en/function.strpos.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+47322
Delete -- http://master.php.net/manage/user-notes.php?action=delete+47322&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47322&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47322&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47322&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47322&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47322&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+47322&report=yes
Search -- http://master.php.net/manage/user-notes.php