Forum Moderators: coopster
Ive read the user comment on php.net but still didnt make much sense to me. Maybe its one of those things that you just suddenly get for no apparent reason.
I was trying to use it for form checking, such as
if (($value == "name") and strspn((trim($_POST[$value])), ",abcdefghijklmnopqrstuvwxyz")) {
$error = "theres an error";
}
Thats the part of the script where the strspn is. Before it is an array of each form field, and then a foreach statement ($arrayname as $value).
<pre>
<?php
$str1 = "42 is the answer, what is the question ...";
$str2 = "1234567890";
$var = strspn($str1, $str2);
print "\$str1: {$str1}\n\$str2: {$str2}\n\n";
print "The first line here is a ruler for counting characters (\$str1 contains 42 characters) ...\n";
print "123456789012345678901234567890123456789012\n";
print "{$str1}\n";
print "Number of characters counted from beginning of \$str1 that were found in \$str2: {$var}\n\n\n";
// Prints 2 (the numbers 4 and 2 are both found in $str2, but the space is not so it stops there
// and returns the count of 2.
print "... now let's change the second string to just search for the letter 'a' ...\n";
$str2 = "a";
$var = strspn($str1, $str2);
print "\$str1: {$str1}\n\$str2: {$str2}\n\n";
print "The first line here is a ruler for counting characters (\$str1 contains 42 characters) ...\n";
print "123456789012345678901234567890123456789012\n";
print "{$str1}\n";
print "Number of characters counted from beginning of \$str1 that were found in \$str2: {$var}\n";
// Prints 0 (zero)
?>
</pre>