Forum Moderators: coopster

Message Too Old, No Replies

strspn - can someone explain it?

         

surrealillusions

9:48 pm on Oct 16, 2008 (gmt 0)

10+ Year Member



Hi all,

I've come across the strspn function and sort of shown how to use it..but yet i dont fully understand how it works.

The php.net page doesnt make much sense to me, so I'm hoping someone on here can explain it in simpleton terms.

I'll be very grateful

:)

PHP_Chimp

7:58 pm on Oct 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you read through the first of the user comments? As that explains the function is a different way.

tumr

8:02 pm on Oct 18, 2008 (gmt 0)

10+ Year Member



It could be instructive if we knew what you were trying to do with it, and tailor our responses accordingly.

surrealillusions

1:49 pm on Oct 21, 2008 (gmt 0)

10+ Year Member



Hi,

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

coopster

4:22 pm on Oct 21, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



There are many ways to edit check form values, but I'm not so certain this function is going to do all that you want/ask. I'll try to explain in code what this function does, you'll have to determine whether or not it is going to work for your form editing ...

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