All categories
    PHPBeginner

    PHP Strings

    Master PHP string functions: measure length, change case, concatenate, replace, find, split, and join text.

    01String functions

    main.php
    <?php
    $s = "Hello, PHP";
    echo strlen($s), "\n";
    echo strtoupper($s), "\n";
    echo strtolower($s), "\n";
    Output
    10
    HELLO, PHP
    hello, php

    02Concatenation

    main.php
    <?php
    $first = "Py";
    $second = "thon";
    echo $first . $second . "\n";
    Output
    Python

    03Replace and find

    main.php
    <?php
    $text = "I like cats";
    echo str_replace("cats", "dogs", $text), "\n";
    echo strpos($text, "like"), "\n";
    Output
    I like dogs
    2

    04Split and join

    main.php
    <?php
    $csv = "red,green,blue";
    $parts = explode(",", $csv);
    echo $parts[1], "\n";
    echo implode(" | ", $parts), "\n";
    Output
    green
    red | green | blue