How to find the position of the first or last occurrence of a substring in a string. You’ll also learn how to find the all positions of a substring in a string (or a multibyte string).
Category: PHP
PHP is a scripting language that’s usually embedded or combined with HTML and has many excellent libraries that provide fast, customized access to DBMSs.
Use the substr() function to extract a portion of a string. Use mb_substr() function instead if you are dealing with a multibyte string.
How to remove leading and trailing whitespace from a string and how to replace line breaks in a string with <br> tags.
Padding Strings
Pad a string on the left, on the right, or on both the left and the right using str_pad() and printf() functions.
Formatting Strings
Earlier we presented the basic method for outputting text with echo and print, and the functions print_r( ) and var_dump( ), which can determine the contents of variables during debugging. PHP provides several other functions that allow more complex and controlled formatting of strings.
Convert HTML Entities and Special Characters
Learn how to encode or decode all or special HTML characters within a string.
Converting Strings into HTML
A commonly used web attack is called Cross-Site Scripting (XSS). For example, a user enters some malicious data, such as JavaScript code, into a web form; the web page then at some point outputs this information verbatim, without proper escaping. Standard examples for this are your blog’s comments section or discussion forms.
Learn how comparison (a string comparison) actually made and how to perform the natural order comparison with stratum() and strnatcasecmp() functions. The natural order comparison treats the string’s numeric parts separately, these functions implement an algorithm that compares alphanumeric strings in the way a human being would.
Comparing Strings
PHP offers four functions for comparing strings: strcmp(), strcasecmp(), strncmp(), and strcasencmp(). These functions return a positive value when the string passed as the first parameter is greater than the second parameter, and a negative value when it is smaller. If both strings are equal then 0 is returned.
How to change the string case?
In this tutorial, you’ll learn how to change the case of a string. For example, capitalize the first character of a string or word or change the case of the whole string to upper or lower case. This tutorial also explains how to alter the case of Multibyte characters.