String Length and Character Counting in PHP
PHP String with Security Considerations - Part 5
Foreword: In this part of the series I talk about String Length and Character Counting in PHP.
By: Chrysanthus Date Published: 29 Oct 2018
Introduction
String Length
The PHP strlen() function returns the number of bytes (characters) in a string. The syntax is:
int strlen ( string $string )
Try the following code:
<?php
$str = "This is a string. Ha ha ha";
$len = strlen($str);
echo $len;
?>
The output is:
26.
If you count the number of characters in the string, the number, 26 will be confirmed.
strlen() returns the length of the string on success, and 0 if the string is empty.
Remark:
strlen() returns NULL when the argument is an array, and an E_WARNING level error is emitted.
Number of Substring Occurrences
You can obtain the number of substring occurrences in the main string. Use the substr_count() function whose syntax is:
int substr_count ( string $haystack , string $needle [, int $offset = 0 [, int $length ]] )
where $length is for the portion of the main string you want to search, beginning from $offset (includes $offset).
The function returns an integer.
Try the following code:
<?php
$subStr = 'cat';
$str = 'A cat is an animal. A cat is a creature.';
$number = substr_count($str, $subStr);
echo $number;
?>
The output is:
2
The strrev() Function
This function reverses the order of the characters in a string. The syntax is:
string strrev ( string $string )
The reversed string is returned. Try this:
<?php
$str = 'I love my work.';
$ret = strrev($str);
echo $str, '<br>';
echo $ret, '<br>';
?>
The output is:
I love my work.
.krow ym evol I
The str_shuffle() Function
You can shuffle the characters of a string. The syntax is:
string str_shuffle ( string $str )
The return value is the shuffled string. Try the following code:
<?php
$str = 'I love my work.';
$ret = str_shuffle($str);
echo $str, '<br>';
echo $ret, '<br>';
?>
My output for now is:
I love my work.
oov.Iwrly k e m
If I try it again, the return string will be different.
Converting a String to an Array
You can convert a string to an index array in order to use array functions on the string characters. The syntax to do this is:
array str_split ( string $string [, int $split_length = 1 ] )
The function returns an array. If $split_length is omitted, each value is a character. If $split_length is 4 for example, then the index array would consist of groups of 4 characters (as values). Try the following code:
<?php
$str = 'Yes my friend';
$arr1 = str_split($str);
$arr2 = str_split($str, 3);
for($i=0; $i<count($arr1); ++$i)
{
echo $i, ' => ', $arr1[$i], ', ';
}
echo '<br><br>';
for($i=0; $i<count($arr2); ++$i)
{
echo $i, ' => ', $arr2[$i], ', ';
}
?>
The output is:
0 => Y, 1 => e, 2 => s, 3 => , 4 => m, 5 => y, 6 => , 7 => f, 8 => r, 9 => i, 10 => e, 11 => n, 12 => d,
0 => Yes, 1 => my, 2 => fr, 3 => ien, 4 => d,
That is it for this part of the series. We take a break here, and continue in the next part.
Chrys
Related Links
Basics of PHP with Security ConsiderationsWhite Space in PHP
PHP Data Types with Security Considerations
PHP Variables with Security Considerations
PHP Operators with Security Considerations
PHP Control Structures with Security Considerations
PHP String with Security Considerations
PHP Arrays with Security Considerations
PHP Functions with Security Considerations
PHP Return Statement
Exception Handling in PHP
Variable Scope in PHP
Constant in PHP
PHP Classes and Objects
Reference in PHP
PHP Regular Expressions with Security Considerations
Date and Time in PHP with Security Considerations
Files and Directories with Security Considerations in PHP
Writing a PHP Command Line Tool
PHP Core Number Basics and Testing
Validating Input in PHP
PHP Eval Function and Security Risks
PHP Multi-Dimensional Array with Security Consideration
Mathematics Functions for Everybody in PHP
PHP Cheat Sheet and Prevention Explained
More Related Links