|
użytkowników online: 65
|
OPINIE UŻYTKOWNIKÓW
|
Mimo, że strony WWW tworzymy już 5 lat zawsze znajdziemy coś ciekawego. Świadczy o tym chociażby nasza aktówka, w której znajduje się kilkadziesiąt porad, z których często korzystamy. Otwarta forma poradnika, czyli możliwość podrzucania tematów oraz wspólny ich rozwój, to nieoceniona pomoc. Uważam, ze abonament roczny jest niewspółmiernie niski do jakości zaprezentowanych materiałów.
Marek Kończal
Internetix.pl
|
|
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net
[Spis]
[A]
[B]
[C]
[D]
[E]
[F]
[G]
[H]
[I]
[J]
[K]
[L]
[M]
[N]
[O]
[P]
[Q]
[R]
[S]
[T]
[U]
[V]
[X]
[W]
[Z]
strlen (PHP 3, PHP 4, PHP 5) strlen -- Get string length Descriptionint strlen ( string string )
Returns the length of the given string.
Przykład 1. A strlen() example |
<?php
$str = 'abcdef';
echo strlen($str); $str = ' ab cd ';
echo strlen($str); ?>
|
|
See also count(), and mb_strlen().
User Contributed NotesHage Yaapa
12-Jan-2006 12:51
Sometimes you really wanna make sure no user edits the 'maxlength' attribute of the HTML page and POSTs a 5 Mb string to your script. Probably, advanced programmers already take precautions, this one is just a very simple tip for beginners on how to check the character lenth of the POST variables in an effective manner.
<?php
$_POST = array_map('strip_tags', $_POST);
$alias = $_POST['alias'];
$name = $_POST['name'];
$status = $_POST['status'];
$year = $_POST['year'];
$exlen = array (
'alias'=>12,
'name'=>30,
'status'=>10,
'year'=>4
);
foreach ($exlen as $key=>$val) {
if (strlen($$key) > $val) {
print 'WARNING: The FBI is looking for you, dude!';
exit;
}
}
?>
Similarly, with the use of Regular Expressions you could check the data type and string format too.
anpaza at mail dot ru
30-Nov-2005 09:58
Here's a better strlen() for UTF-8 strings that doesn't access the byte past end of the string (on which newer PHP barfs):
function strlen_utf8 ($str)
{
$i = 0;
$count = 0;
$len = strlen ($str);
while ($i < $len)
{
$chr = ord ($str[$i]);
$count++;
$i++;
if ($i >= $len)
break;
if ($chr & 0x80)
{
$chr <<= 1;
while ($chr & 0x80)
{
$i++;
$chr <<= 1;
}
}
}
return $count;
}
ralf at bruderherz dot com
17-Nov-2005 12:31
Why not:
function b_strlen($str){
$ret = 0;
for($i = 0; $i < strlen($str); $i++){
if( (ord($str{$i}) >= 0 && ord($str{$i}) <= 127) || ord($str{$i}) >= 192 ) $ret++;
}
return $ret;
}
It's easier I think...
Fabi K
30-Oct-2005 12:08
So here a short script that will count the letters in an utf8-string. This will work for the whole utf-8 character set.
function strlen_utf8(&$str) {
while($str[$i]) {
$cnt = $cnt = countLeadingBits(ord($str[$i]));
$i += $cnt>0 ? $cnt : 1; // skip following bytes if the char was bigger than a Byte.
$diff += $cnt>0 ? $cnt-1 : 0; // counting every skipped Byte.
}
return ($i - $diff);
}
define(FIRST_BIT, 0x80);
/**
This function counts the leading 'on'-Bits in a Byte.
@param char The byte to analyse. Must be a number. Use ord() to analyse characters.
@return The number of leading bits that where set (1).
*/
function countLeadingBits($char) {
$char = $char & 255; // deleting 'on'-bits that where greater than 2^8 => every number is less or equal 0xff(255)
//echo sprintf("%08d ", decbin($char));
if( (FIRST_BIT & $char) === FIRST_BIT) { // a leading Bit is found
return countLeadingBits($char << 1) + 1;
}
return 0;
}
$testdata = "abcABC123
|