Skip to main content

String


Array

explode()

Syntax: explode(separator,string,limit)

Konvertiert string zu array

  • separator als trennzeichen zwischen den Elementen angeben

    $str = "Hello World.";
    print_r(explode(" ", $str));
    // -> Array ( [0] => Hello [1] => World. )

    $num = 'one,two,three';

    print_r(explode(',', $num));
    // -> Array ( [0] => one [1] => two [2] => three )
  • Anzahl der Elemente begrenzen mit limit

    print_r(explode(',', $str, 0));
    print "<br>";
    // -> Array ( [0] => one,two,three,four )

    print_r(explode(',', $str, 2));
    print "<br>";
    // -> Array ( [0] => one [1] => two,three,four )

    print_r(explode(',', $str, -1));
    // -> Array ( [0] => one [1] => two [2] => three )

str_split()

string in chars zerlegen und in ein array umwandeln

print_r(str_split("Hello"));
// -> Array ( [0] => H [1] => e [2] => l [3] => l [4] => o )

ASCII

chr()

ASCII zu Zeichen

echo chr(66);
// -> B

ord()

Zeichen zu ASCII

echo ord("A");
// -> 65

strlen()

Zeichenlänge eines String bestimmen

echo strlen("Hello");
// -> 5

str_replace()

Alle Wort / Zeichen in einem string ersetzen

echo str_replace("world", "Peter", "Hello world!") . "<br>";
// -> Hello Peter!

echo str_replace("o", "💩", "Hello world!");
// -> Hell💩 w💩rld!

strrev()

string rückwärts ausgeben

echo strrev("!dlroW olleH");
// -> Hello World!

str_shuffle()

chars im string zufällig mischen

echo str_shuffle("Hello World");
// z.B. -> l lrloHWode

strtolower() / strtoupper()

String in Klein- / Großbuchstaben umwandeln

  echo strtolower("HELLO WORLD");
// -> hello world!
echo strtoupper("hello world!");
// -> HELLO WORLD!

Danger:

Umlaute funktionieren nicht!

Info:

funzt...

  • toUpper

    function strtoupper_utf8($str)
    {
    $from = array('ä', 'ö', 'ü');
    $to = array('Ä', 'Ö', 'Ü');

    return strtoupper(str_replace($from, $to, $str));
    }

    $str = 'äöü';

    echo strtoupper_utf8($str);
    // Ausgabe ÄÖÜ
  • toLower

    function strtolower_utf8($str)
    {
    $from = array('Ä', 'Ö', 'Ü');
    $to = array('ä', 'ö', 'ü');

    return strtoupper(str_replace($from, $to, $str));
    }

    $str = 'ÄÖÜ';

    echo strtolower_utf8($str);
    // Ausgabe äöü

substr()

Syntax: substr(string,start,length)

Teile eines string ausschneiden

  • nur mit start
    echo substr("Hello world", 6) . "<br>";
    // -> world
    echo substr("Hello world", -3) . "<br>";
    // -> rld
  • mit start und lenght
    echo substr("Hello world", 3, 5) . "<br>";
    // -> lo wo
    echo substr("Hello world", -10, -2) . "<br>";
    // -> ello wor

trim()

Zeichen am Anfang und Ende (keine mittigen) entfernen

$str = "_._Hello World!_._";

echo trim($str, "_._");
// -> Hello World!

ucfirst()

Ersten Buchstaben eines string groß schreiben

echo ucfirst("hello world!");
// -> Hello world!

Danger:

Funktioniert nicht bei Sonderzeichen wie Umlauten, abhängig von

Info:

funzt..

$str = "ähre";

function myucfirst($str)
{
$fc = mb_strtoupper(mb_substr($str, 0, 1));
return $fc . mb_substr($str, 1);
}

// -> Ähre