PHP String functions
String Manipulation Functions
PHP provides numerous built-in functions for manipulating strings. Here are some common ones:
strpos()
: Find the position of the first occurrence of a substring.<?php $haystack = "Hello, World!"; $needle = "World"; $position = strpos($haystack, $needle); echo $position; // Outputs: 7 ?>
substr()
: Return a part of a string.<?php $string = "Hello, World!"; $substring = substr($string, 7, 5); echo $substring; // Outputs: World ?>
str_replace()
: Replace all occurrences of a search string with a replacement string.<?php $string = "Hello, World!"; $newString = str_replace("World", "PHP", $string); echo $newString; // Outputs: Hello, PHP! ?>
trim()
: Remove whitespace (or other characters) from the beginning and end of a string.<?php $string = " Hello, World! "; $trimmedString = trim($string); echo $trimmedString; // Outputs: Hello, World! ?>
strtoupper()
: Convert a string to uppercase.<?php $string = "Hello, World!"; $upperString = strtoupper($string); echo $upperString; // Outputs: HELLO, WORLD! ?>
strtolower()
: Convert a string to lowercase.<?php $string = "Hello, World!"; $lowerString = strtolower($string); echo $lowerString; // Outputs: hello, world! ?>
ucfirst()
: Capitalize the first character of a string.<?php $string = "hello, world!"; $capitalizedString = ucfirst($string); echo $capitalizedString; // Outputs: Hello, world! ?>
strlen()
: Get the length of a string.<?php $string = "Hello"; echo strlen($string); // Outputs: 5 ?>
Multiline Strings
For multiline strings, you can use heredoc or nowdoc syntaxes.
Heredoc:
- Allows for the creation of multi-line strings without escaping quotes.
- Syntax:
<<<EOT
Example:
<?php $string = <<<EOT This is a multiline string using heredoc. EOT; echo $string; ?>
Nowdoc:
- Similar to heredoc but does not parse variables.
- Syntax:
<<<'EOT'
Example:
<?php $string = <<< 'EOT' This is a multiline string using nowdoc. Variables like $variable will not be parsed. EOT; echo $string; ?>
String Conversion Functions
str_split()
: Convert a string into an array of characters.<?php $string = "Hello"; $array = str_split($string); print_r($array); // Outputs: Array ( [0] => H [1] => e [2] => l [3] => l [4] => o ) ?>
explode()
: Split a string by a specified delimiter.<?php $string = "apple,banana,orange"; $array = explode(",", $string); print_r($array); // Outputs: Array ( [0] => apple [1] => banana [2] => orange ) ?>
implode()
: Join array elements into a single string.<?php $array = array("apple", "banana", "orange"); $string = implode(", ", $array); echo $string; // Outputs: apple, banana, orange ?>
Summary
- Defining Strings: Strings are enclosed in single or double quotes.
- Concatenation: Combine strings using the
.
operator. - Manipulation Functions: PHP provides a variety of functions for working with strings, including searching, replacing, and formatting.
- Multiline Strings: Use heredoc or nowdoc syntaxes for multi-line text.
- Conversion Functions: Convert strings to arrays and vice versa using functions like
str_split()
,explode()
, andimplode()
.