PHP String functions


String Manipulation Functions

PHP provides numerous built-in functions for manipulating strings. Here are some common ones:

  1. 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 ?>
  2. substr(): Return a part of a string.

    <?php $string = "Hello, World!"; $substring = substr($string, 7, 5); echo $substring; // Outputs: World ?>
  3. 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! ?>
  4. 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! ?>
  5. strtoupper(): Convert a string to uppercase.

    <?php $string = "Hello, World!"; $upperString = strtoupper($string); echo $upperString; // Outputs: HELLO, WORLD! ?>
  6. strtolower(): Convert a string to lowercase.

    <?php $string = "Hello, World!"; $lowerString = strtolower($string); echo $lowerString; // Outputs: hello, world! ?>
  7. ucfirst(): Capitalize the first character of a string.

    <?php $string = "hello, world!"; $capitalizedString = ucfirst($string); echo $capitalizedString; // Outputs: Hello, world! ?>
  8. 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.

  1. 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; ?>
  2. 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

  1. 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 ) ?>
  2. 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 ) ?>
  3. 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(), and implode().