PHP 5 Strings

A string is a set of characters. In PHP, you may consider it to be an array of characters, because you may access a certain character in the string based on the zero-based index of it, just like with an array. In this chapter of the tutorial, we will work a bit with strings in a variety of ways, to show you the possibilities.

Search For a Specific Text Within a String

The PHP strpos() function searches for a specific text within a string. If a match is found, the function returns the character position of the first match. If no match is found, it will return FALSE. The example below searches for the text "world" in the string "Hello world!"

Example -

Defining strings

Defining strings in PHP is really easy. Simply wrap a piece of text with either single or double quotes, and you have a string.

Example -

Replace Text Within a String

The PHP str_replace() function replaces some characters with some other characters in a string.

Example -