Determining
the Length of a String
The strlen() function is used to
determine the length, in bytes, of a string. Note that strlen(), like most
string functions, is bin ary-safe. This means that all characters in the string
are counted, regardless of their value. (In some languages (notably C), some
functions are designed to work with “zero-terminated” strings, where the NUL character
is used to signal the end of a string. This causes problems when dealing with
binary objects, since bytes with a value of zero are quite common; luckily,
most PHP functions are capable of handling binary data without any problem.)
Transforming
a String
The strtr() function can be used to
translate certain characters of a string into other characters—it is often used
as an aid in the practice known as transliteration to transform certain
accented characters that cannot appear, for example, in URLs or e-mail address
into the equivalent unaccented versions:
// Single character
version
echo strstr (’abc’,
’a’, ’1’); // Outputs 1bc
// Multiple-character
version
$subst = array (
’1’ => ’one’,
’2’ => ’two’,
);
echo strtr (’123’,
$subst); // Outputs onetwo3
Using
Strings as Arrays
You can access the individual
characters of a string as if they were members of an array. For example:
$string = ’abcdef’;
echo $string[1]; //
Outputs ’b’
This approach can be
very handy when you need to scan a string one character at a time:
$s = ’abcdef’;
for ($i = 0; $i <
strlen ($s); $i++) {
if ($s[$i] > ’c’) {
echo $s[$i];
}
}
Note that string character indices
are zero-based—meaning that the first character of an arbitrary string $s has an
index of zero, and the last has an index of strlen($s)-1.
Comparing,
Searching and Replacing Strings
Comparison is, perhaps, one of the
most common operations performed on strings. At times, PHP’s type-juggling
mechanisms also make it the most maddening—particularly because strings that
can be interpreted as numbers are often transparently converted to their
numeric equivalent. Consider, for example, the following code:
$string = ’123aa’;
if ($string == 123) {
// The string equals
123
}
You’d expect this comparison to
return false, since the two operands are most definitely not the same. However,
PHP first transparently converts the contents of $string to the integer 123,
thus making the comparison true. Naturally, the best way to avoid this problem
is to use the identity operator === whenever you are performing a comparison
that could potentially lead to type-juggling problems.
In addition to comparison operators,
you can also use the specialized functions strcmp() and strcasecmp() to match
strings. These are identical, with the exception that the former is case-sensitive,
while the latter is not. In both cases, a result of zero indicates that the two
strings passed to the function are equal:
$str = "Hello
World";
if (strcmp($str,
"hello world") === 0) {
// We won’t get here,
because of case sensitivity
}
if (strcasecmp($str,
"hello world") === 0) {
// We will get here,
because strcasecmp()
// is case-insensitive
}
A further variant of strcasecmp(),
strcasencmp() allows you to only test a given number of characters inside two
strings. For example:
$s1 = ’abcd1234’;
$s2 = ’abcd5678’;
// Compare the first
four characters
echo strcasencmp ($s1,
$s2, 4);
0 Comment to " PHP Strings and Pattern: Length, transforming, array as string "
Post a Comment