office microsoft outlook manage tips Microsoft Windows 7 Ultimate 64-bit microsoft office final exam microsoft office turorials Microsoft Office Visio Professional 2007 microsoft mouse driver for windows xp windows media center microsoft english Microsoft Windows 7 Home Premium 64 Bit microsoft windows start up tone microsoft office xp pro with frontpage Microsoft Windows 7 Professional beta information microsoft office system office xp microsoft outlook sp3 vista Microsoft Office Outlook 2007 microsoft office for windows xp microsoft office x mac Microsoft Windows 7 Ultimate (32 bit) microsoft windows user microsoft office 2007 training video Microsoft Windows XP Professional SP3 32-bit microsoft office setup cannot continue microsoft remote tools framework windows Microsoft Windows 7 Professional 64 Bit microsoft office standard 2003 key generator microsoft windows media player upgrade Microsoft Office 2003 Professional microsoft office 2003 upgrade requirements microsoft windows me repair Microsoft Office Project Professional 2003 microsoft windows network not accessible
msgbartop
I will happily conduct a FREE basic web security scan for any genuine organization interested in my services to point out whether or not I can find vulnerabilities in your application. Just contact me.
Need a PHP Programmer, PHP staff or project manager? Contact me now.
msgbarbottom

24 Nov 09 PHP Programmer – strlen, count and substr

PHP Developer – strlen, count, and substr

The strlen function retuns the length, i.e. number of characters in a string:  int strlen(string s)

count will get the number of elements in an array:  int count(array a)

substr will return a “subset” of a string, string substr(string s, int start, [int len]);
<?php
$s = “test string”;
echo “String length is: ” . strlen($s);
?>

Will return:  String length is: 11

Why would you care how long a string is? Well, for many reasons, one being that you might wish to iterate through each character of a string to perform a certain conditional check or operation on each character. Alternatively, you might want to check that a certain string is not over a given size, and if so, shorten it. Here’s a common example that shows these three common functions together:

<?php
$myarray = Array("This is a very long string", "short string", "some text", "some more text to be shortened");
define(MAXLEN, 20); //maximum permitted string length
$num = count($myarray); //Get the number of elements in the array
for ($ctr = 0; $ctr < $num; $ctr++)
{
    if (strlen($myarray[$ctr]) > MAXLEN)
    {
        echo substr($myarray[$ctr], 0, (MAXLEN - 3)) . "...\n";
    } else {
        echo $myarray[$ctr] . "\n";
    }
}
?>

The above will output:
This is a very lo…
short string
some text
some more text to…

This could be used in an instance where we only want to show a predefined “taster”, i.e. replacing … with “(more)” or similar. Alternatively, ensuring that text does not overflow a “<div>” element in a particular instance

Tags: , , , ,



Leave a Comment

You must be logged in to post a comment.