05 – PHP Strings

In PHP, as in any other language, there are built in functions used to manipulate strings. Here are few of the most commonly used PHP functions for manipulating strings:

  • strlen() – gets the length of a string
    $a = "This is PHP";
    echo strlen($a);

    It outputs 11.

  • str_word_count() – counts the number of words in a string
    $a = "This is PHP";
    echo str_word_count($a);

    It outputs 3.

  • revstr() – reverses a string
    $a = "This is PHP";
    echo strrev($a);
  • It outputs ” PHP si sihT”.

  • strpos() – searches for a text within a string
    $a = "This is PHP";
    echo strpos($a, "is");

    It outputs 2 (The index where the word “is” is found.

  • str_replace() – replaces text within a string
    $a = "This is PHP";
    echo str_replace("PHP", "not Python", $a);

    It outputs “This is not Python”.

Leave a Reply