strlen(): Length of the string

<?Php
echo strlen('plus2net.com'); // Ouptut is 12
?>
Output is the length of the string in number of bytes.
This function is required for form input validation. We can check if any input field is entered blank.

PHP function strlen returns number of bytes inside the string including blank space. If you want blank space to be removed then you can apply string replace function str_replace to the string before applying strlen.
<?Php 
$string="This is my string";
echo "Length of the string = ".strlen($string);
?>
The output of above code is 17

Length of the string after removing blank spaces

We will use str_replace function to search and replace all occurrences of blank space.
<?Php 
$string="This is my string";
$string=str_replace(" ","",$string);
echo "Length of the string = ".strlen($string);
?>
The output of above code is 14

Maximum and minimum length of string

While checking the Userid of signup script we can use this to find out the length of the string. Here our string should be of minimum 6 and maximum 15 character length.

$string="abcdababcdababcs"; // Add or remove string chars to check output
if(strlen($string) >=6 and strlen($string) <=15) { echo "<br><br> length is wihtin 6 and 15 "; } else { echo "<br><br> Not required length ";}

Null variable and strlen()

<?Php
$my_var1=null;
$my_var2='';

echo strlen($my_var1); // Ouptut is 0
echo strlen($my_var2); // Output is 0
?>
Checking null variable by using strlen() may not give desire result.

strlen() returns number of bytes rather than number of characters.
strrev(): PHP string reverse
String Functions strrev(): PHP string reverse nl2br() To add Line break
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    elyn espanola

    05-02-2012

    what are the php codes of counting different letters from 2 words that given by the users and each word will be equal to the number of letters that are different to the other words. for example: cat=1 bratt=2 since in the word cat,there is no letter c in the other word so it equals to 1, then in the second word, there is no letter b and r in the first word so it equals to 2,tnx,,i need ur ideas.
    dickson

    07-12-2012

    nice stuff. home 4 the best. tank

    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer