超越PHP PHP动态 | 经典文章 | CLASS | 相关下载 | 常见问题 | FORUM | WIKI | 在线手册
Site search:    
<strcollstrip_tags>
Last updated: Fri, 22 Jun 2007

strcspn

(PHP 3>= 3.0.3, PHP 4 , PHP 5)

strcspn --  Find length of initial segment not matching mask

Description

int strcspn ( string str1, string str2 [, int start [, int length]])

Returns the length of the initial segment of str1 which does not contain any of the characters in str2.

As of PHP 4.3.0, strcspn() accepts two optional integer parameters that can be used to define the start position and the length of the string to examine.

注: 该函数适用于二进制对象!

See also strspn().




add a note add a note User Contributed Notes
strcspn
info at owensoft dot net
25-Jan-2003 08:09
this function checks if a string contains special characters excluding "_" and "-".  you can also easy customise it.

function vstr2 ($str) { //
if ( strcspn($str, '~`!@#$%^&*()+{}[]| \\:;<>,./?+=' ) != strlen($str) ) return false;
if ( !ctype_print ($str) ) return false;

return true;
}
netanakroas at freemail dot hu
20-Dec-2002 05:42
When you need something like... check a sting, comapre whit an another string, and if the sting's character is not on the another sting's character, then... anything. Then you should use this function:
function strchk ($in,$eng){
$i=0;
$fail=0;  
while ($i<strlen($in))
{
$temp="";
$f=0;
$ok=0;
$temp=substr($in,$i,1);
while ($f<strlen($eng))
{
$temp2="";
$temp2=substr($eng,$f,1);
if ($temp2==$temp){$ok=1;}
$f++;
}
$i++;
$f=0;
if ($ok==0){$fail=1;break;}

}
return $fail;
}

---------------------------------
Usage:
int value strchk(string to check, possible characters string);

When all of the string's characters are can be find in the "possible characters sting", you will recive a 0. When not, you recive 1.

by NetAnakroas
php at nospam dot mike2k dot com
20-Nov-2002 11:39
this is an awesome function if you want to test letter-based modes or roles, i.e.:

function check_access($required_roles,$user_roles) {
  if(strcspn($required_roles,$user_roles) == strlen($required_roles)) {
     print "no access. do your lockout routine here or die().";
  } else {
     print "access granted.";
  }
}

so say you have roles for u = user, a = admin, s = superadmin, and you want to make sure the user is an admin OR superadmin, and the user is only "u" - it will return false:

checkaccess("as",$user_roles)

but if they have "a" it will be true.. i'm sure you could use the strspn() function to make sure the ANDs work, but i only used ORs in my situation. almost kinda like doing bitwise on the alphabet.

<strcollstrip_tags>
 Last updated: Fri, 22 Jun 2007
view source | feedback | send page | sitemap | aboutus   
Copyright ® 2002-2003 PHPE.NET. All rights reserved
Last updated:2002-11-22