PHP Basic String Manipulation Techniques
Lesson Overview
Welcome! In this lesson, we'll delve into the basic string manipulation features of PHP, which include string tokenization, string concatenation, trimming whitespace from strings, and type conversion operations.
Tokenizing a String in PHP
In PHP, we can use the explode function to tokenize a string, essentially splitting it into smaller parts or 'tokens'.
Using explode function:
In the example above, we use a space as a delimiter to split the $sentence into words. This operation will print each word in the sentence on a new line.
Exploring String Concatenation
In PHP, the . operator or implode function can be used to concatenate strings into a larger string:
Using the . Operator:
Using implode:
In the example above, implode is used to concatenate all the elements of an array into a single string.
Trimming Whitespaces from Strings
In PHP, the trim function can remove both leading and trailing whitespaces from a string:
In this example, trim is used to remove leading and trailing whitespaces from a string.
PHP Type Conversions
We can convert strings to numbers using functions like intval (string to integer) and floatval (string to float), and other data types to strings using simple concatenation or casting:
In this code, we use intval, floatval, and simple casting for type conversions.
