Читать книгу PHP Programming for Beginners. Key Programming Concepts. How to use PHP with MySQL and Oracle databases (MySqli, PDO) - Sergey D Skudaev - Страница 10
Useful string functions
ОглавлениеPHP has many built-in string functions. String length can be determined with strlen () function.
$lastname=“Shakespeare”;
$len=strlen ($lastname);
echo “Length=”. $len.”<br>”;
Output: Length=11
The trim () function trims space characters from the beginning and the end of a string. This function is useful because a user may inadvertently enter space characters while typing in a login or password. To prevent this from occurring, trim every user input string as in the following example:
$lastname =' Shakespeare”;
echo “Before trim:”. strlen ($lastname).”<br>”;
$lastname=trim ($lastname);
echo “After trim:”. strlen ($lastname).”<br>”;
Output:
Before trim 13
After trim 11
As you can see, I’ve intentionally entered a space at the beginning and end of the last name and its length became longer by two characters. After trimming these spaces, the length was restored to 11 characters.
The string position function strpos () returns the position of the first occurrence of a substring in the string.
For example, echo strpos ($lastname, ‘sp’) will return 5.
You might expect it to return 6 because sp is in the 6th position; however, it returns 5
because the count starts at 0.
echo strpos ($lastname, “Shake’); will return 0.
After execution, echo strpos ($lastname, ‘shake’); nothing will be displayed in the browser because it returns Boolean false. Lower case ‘shake’ has not occurred in the “Shakespeare’. To confirm this, execute the following code:
if (strpos ($lastname, ‘shake’) ===false)
echo “return false <br>”;
Output: return false
The strpos is a case sensitive function. The stripos is a case insensitive function.
echo stripos ($lastname, ‘shake’); will return 5.
The strrpos () – Find the position of the last occurrence of a substring in a string.
echo strrpos ($lastname, ‘e’) will return 10.
The substr () function will return a substring of a string.
echo substr (“William Shakespeare’, 8); will return Shakespeare
echo substr (“William Shakespeare’, 8, 5); will return Shake.
It starts counting from 0 and returns that piece of substring starting from the 8th character.
The second parameter, if it is present, determines the substring length. This explains why
echo substr (“William Shakespeare’, 8, 5); will return Shake.
If the second parameter is not present, all that portion of string is returned.
This is why echo substr (“William Shakespeare’, 8); will return Shakespeare
The str_replace () is another important string function. This function replaces all occurrences of the search string with a replacement string.
For example, if a user entering text containing single or double quotation marks into a form, it can create problems with the database. To prevent this from happening, using the str_replace function, we can replace all occurrences of single quotation marks in the input text.
$comments=“this is comments with a ‘single quotation’ marks”;
echo “before str_replace:”. $comments.”<br>”;
$comments=str_replace (” ‘”, "´”, $comments);
echo “after str_replace:”. $comments.”<br>”;
Output:
before str_replace: this is comments with a ‘single quotation’ marks after str_replace: this is comments with a “single quotation’ marks
When viewing the web page source, you will see that each single quotation mark has been replaced by the ´ code.