Portrety Uliczne Nieznajomych - zobacz wyjątkową galerię portretów z warszawskich ulic
ZALOGUJ SIĘ
login:
hasło:
przypomnij hasło
załóż konto użytkownika
(i zobacz kilka porad gratis)
   
WYSZUKIWARKA I DZIAŁY
całe porady  tytuły
zaznacz działy do przeszukania
(brak wyboru = wszystkie działy)
PHP
MySQL >
PostgreSQL
SQLite
Perl
Java
XML
XSLT
XPath
WML
SVG
RegExp
Wyszukiwarki
Ochrona
VBScript
Google Plus
XHTML/CSS
JavaScript
Grafika
Flash
Photoshop
Windows
Linux
Bash
Apache
Procmail
E-biznes
Explorer
Opera
Firefox
Inne porady
   
KURSY, DOKUMENTACJE
Własne:
XHTML/CSS
JavaScript
ActionScript
WML, RSS, SSI
Pozostałe:
PHP
MySQL
Java API
więcej...
   
użytkowników online: 65
W CZYM MOGĘ POMÓC?


   
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

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

Description

int strlen ( string string )

Returns the length of the given string.

Przykład 1. A strlen() example

<?php
$str
= 'abcdef';
echo
strlen($str); // 6

$str = ' ab cd ';
echo
strlen($str); // 7
?>

See also count(), and mb_strlen().




User Contributed Notes

Hage 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

// ALWAYS clean the POST variables of any HTML tags first.
// And here we do it in one easy step.
 
$_POST = array_map('strip_tags', $_POST);
 
// These are the POST variables in the example
 
$alias = $_POST['alias'];
 
$name = $_POST['name'];
 
$status = $_POST['status'];
 
$year = $_POST['year'];
 
// We create an array that contains the expected character length
// for each POST variable
 
$exlen = array (
    
'alias'=>12,
    
'name'=>30,
    
'status'=>10,
    
'year'=>4
);
 
// Now check if any of them exceeds the expected length
 
foreach ($exlen as $key=>$val) {
     if (
strlen($$key) > $val) {
          
// The user has definitely edited the HTML! He has a lot of time, could be bad.
         // This section can edited according to your needs - very simple to complex.
         // Log the event or send an e-mail to the admin at the basic.
         // However, in this example we just print a warning.   
        
print 'WARNING: The FBI is looking for you, dude!';
         exit;
        
// The best part is that the script won't look for any other
         // POST variables other than the ones which we are expecting already.
    
}
}
?>

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

 

 
  © 1996-2012 & Reporter.plmiejscao serwisieabonamentwarunki korzystaniaRSSkontakt