Web студия "GrandView"
  Главная   Написать Контакты
   
   
О проекте
Руководство php
 

strlen

(PHP 3, PHP 4, PHP 5)

strlen -- Возвращает длину строки

Описание

int strlen ( string string )

Возвращает длину строки string.

Пример 1. Пример использования strlen()

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

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

См. также описание функций count() и mb_strlen().



strnatcasecmp> <stristr
Last updated: Sat, 27 Jan 2007
 
add a note add a note User Contributed Notes
strlen
mail4adry at inwind dot it
21-Aug-2007 01:53
my last post was a biiiiig mistake. This is the right way to display a few words of an html formatted text, which is based on the functions of the previous post, strllen_Html and strlen_noHtml, that returns the string length counting tag-characters and non-tag-characters.

This can be useful to put a preview of an article on the front page when the rest of the article is displayed on another page:

function getPreviewText($text) {
    // Strip all tags
    $desc = strip_tags(html_entity_decode($text), "<a><em>");
    $charlen = 0; $crs = 0;
    if(strlen_HTML($desc) == 0)
        $preview = substr($desc, 0, 69);
    else
    {
        $i = 0;
        while($charlen < 80)
        {
            $crs = strpos($desc, " ", $crs)+1;
            $lastopen = strrpos(substr($desc, 0, $crs), "<");
            $lastclose = strrpos(substr($desc, 0, $crs), ">");
            if($lastclose > $lastopen)
            {
                // we are not in a tag
                $preview = substr($desc, 0, $crs);
                $charlen = strlen_noHTML($preview);
            }
            $i++;
        }
    }
    return $preview."&#8230;"
}

will display text cut as near as possible to character 80 respecting each <a> and <em> tags and ending with ...
mail4adry at inwind dot it
21-Aug-2007 09:39
// Strip all tags except links
$desc = strip_tags(html_entity_decode($description), "<a>");
// Cut the line to the right length
if(strlen_nohtml($desc) > 72)
      $desc = substr($desc, 0 , 71+strlen_html($desc))."&#8230;";

output something like a text interrupted by ... at car #71 containing active links
mail4adry at inwind dot it
21-Aug-2007 09:28
/**
 * return length of a string regardeless of html tags in it
 *
 * @param string $html
 * @return string
 */
function strlen_noHtml($string){
    $crs = 0;
    $charlen = 0;
    $len = strlen($string);
    while($crs < $len)
    {
        $offset = $crs;
        $crs = strpos($string, "<", $offset);
        if($crs === false)
        {
           $crs = $len;
           $charlen += $crs - $offset;
        }
        else
        {
            $charlen += $crs - $offset;
            $crs = strpos($string, ">", $crs)+1;
        }
    }
    return $charlen;
}

/**
 * return length of a string regarding html tags in it
 *
 * @param string $html
 * @return string
 */
function strlen_Html($string){
    $crs = 0;
    $charlen = 0;
    $len = strlen($string);
    while($crs < $len)
    {
        $scrs = strpos($string, "<", $crs);
        if($scrs === false)
        {
           $crs = $len;
        }
        else
        {
            $crs = strpos($string, ">", $scrs)+1;
            if($crs === false)
                $crs = $len;
            $charlen += $crs - $scrs;
        }
    }
    return $charlen;
}

Example:

$text = "<p>Test 'a' paragraph.</p><!-- Comment --> Other text";

echo "strlen without HTML chars:".strlen_noHtml($text);
echo "<br>";
echo "strlen of HTML chars:".strlen_html($text);

Will output:
strlen without HTML chars:30
strlen of HTML chars:23
Mike Rosoft
03-Jul-2007 07:12
The function "empty" tests whether a variable will evaluate as FALSE; the string '0' (as well as the number 0) does.

Another simple way of testing for an empty string is:

 if($string=='') foo();
  else bar();

This will match an empty string and anything that converts to it (such as NULL/unset variable), but not the number zero as either a number or a string.
bradmwalker at cableone dot net
01-Jul-2007 03:48
want a predicate that tests a string for emptiness? use strlen instead of empty(). strlen only returns a false-equivalent value for ''.

example:

// takes string_array and returns an array without any values w/empty strings
function filter_empties ($string_array) {
    // note: the immensely retarded empty() function returns true on string '0'
    // use strlen as empty string predicate
    return count($string_array) ? array_filter ($string_array, 'strlen') : $string_array;
}
topera at gmail dot com
27-Jun-2007 08:32
//------------------------------------------
// This function returns the necessary
// size to show some string in display
// For example:
// $a = strlen_layout("WWW"); // 49
// $a = strlen_layout("..."); // 16
// $a = strlen_layout("Hello World"); // 99
//------------------------------------------
function strlen_pixels($text) {
    /*
        Pixels utilized by each char (Verdana, 10px, non-bold)
        04: j
        05: I\il,-./:; <espace>
        06: J[]f()
        07: t
        08: _rz*
        09: ?csvxy
        10: Saeko0123456789$
        11: FKLPTXYZbdghnpqu
        12: A
Новости
11 июля 2007
Сайт запущен
© 2007 info@grandviewstudio.com
Z058440144362 Z348613067571