PHP Tutorials – Strings (Part I)
In this PHP Tutorial you will learn about Strings viz The heredoc syntax, String length, String position, Strings comparison, String search and Substring selection
The heredoc syntax:
PHP has another way to specify strings called heredoc syntax. The heredoc syntax is very useful for specifying large text. The heredoc syntax is very useful for specifying text that contains quotes and double quotes, because they don’t need to be escaped. Heredoc syntax starts with a <<< and a label which is an identifier, and ends with the identifier label,
for example:
echo <<<NAMEFORM
<FORM METHOD=”POST” ACTION=”{$_ENV[‘PHP_SELF’]}”>
<INPUT TYPE=”TEXT” NAME=”FIRSTNAME” VALUE=”$firstname”>
<INPUT TYPE=”SUBMIT” NAME=”SUBMIT” VALUE=”SUBMIT”>
</FORM>
NAMEFORM;
String length:
To find the string length, use the function strln(). strlen takes one argument, which is the string.
Example:
<?php
$s = “This is a string”;
echo strln($s);
?>
This will print 16
String position:
To get a position of a string inside a string, use the function strops(). strpos() takes two arguments, the string to search in, and the string to search for.
Example:
<?php
$s = “This is a string”;
echo strln(‘Location of r is ‘ . strops($s, ‘r’));
?>
This will print 12, r is the 13th character of the string, but strings index start with zero.
Strings comparison:
To compare two string for equality, you can use ==.
PHP provide a function for strings comparison, the function is strcmp().
strcmp() takes two arguments, these are the two strings to be compared, the comparison returns one of the following values:
• A negative number if the first string is less than the second string.
• A positive number if the second string is less than the first string.
• Zero if both are identical.
Example:
<?php
$s1 = “This is a string”;
$s2 = “This is a string”;
if(strcmp($s1, $s2) === 0) {
echo ‘Strings are equal’;
} else {
echo ‘Strings are not equal’;
}
?>
If you want to neglect the characters case, use the function strcasecmp().
String search:
To search for a string inside another string, use the function strstr().
strstr() takes two arguments, the string to search in, and the string to search for.
strstr() returns on of the following values:
• If the string is found, it will return the part of the first string that starts with the second string.
• If the string is not found, it will return false.
Example:
<?php
$s1 = “This is a string”;
$s2 = “is”;
echo strstr($s1, $s2);
?>
This will print ‘is a string’.
If you want to neglect the characters case, use the function strtistr().
Substring selection:
To select a portion of a string, use the function substr().
substr() takes two or three arguments, the string to get a portion of it, an integer that defines the start index of the portion, and the optional third argument is the end index of the portion.
Substr() returns one of the following values:
• If two arguments are used, it will return the portion of the string starting from the given index to the end of the string.
• If three arguments are used, it will return the portion of the string starting from the index specified by the second arguments, and with the length specified by the third argument.
Example:
<?php
$s1 = “This is a string”;
echo substr($s1, 5);
?>
This will print ‘is a string’
<?php
$s1 = “This is a string”;
$s2 = “is”;
echo strstr($s1, 5, 2);
?>
This will print ‘is’