Funzioni stringhe chr • Restituisce il carattere di un valore ascii dato. Per vedere l’elenco dei codici ascii clicca Qui Esempio • <?php echo chr(52)."<br />"; echo chr(052)."<br />"; echo chr(0x52)."<br />"; ?> • The output of the code above will be: • 4 * R echo Restituisce stringhe sullo schermo o il contenuto di una variabile echo “ciao”; //ciao $ciao=“ciao”; echo $ciao; //ciao explode The explode() function breaks a string into an array. • Example • In this example we will break a string to an array: • <?php • • $str = "Hello world. It's a beautiful day."; print_r (explode(" ",$str)); ?> The output of the code above will be: Array ( [0] => Hello [1] => world. [2] => It's [3] => a [4] => beautiful [5] => day. ) Ltrim • La funzione rimuove gli spazi bianchi e altri caratteri predefiniti dal lato sinistro di una stringa. Esempio <html> <body> <?php $str = " Hello World!"; echo "Without ltrim: " . $str; echo "<br />"; echo "With ltrim: " . ltrim($str); ?> <body> <html> Risultato Without ltrim: Hello World! With ltrim: Hello World! Rtrim • La funzione rimuove gli spazi bianchi e altri caratteri predefiniti partendo dal lato destro di una stringa. Esempio • <html> <body> <?php $str = "Hello World! "; echo "Without rtrim: " . $str; echo "<br />"; echo "With rtrim: " . rtrim($str); ?> <body> <html> • Risultato sul browser • Without rtrim: Hello World! With rtrim: Hello World! print • Restituisce una o più stringhe • Primo esempio • <?php • • • • $str = "Who's Kai Jim?"; print $str; print "<br />"; print $str."<br />I don't know!"; ?> Il risultato sarà: Who's Kai Jim? Who's Kai Jim? I don't know! Esempio 2 <?php print "This text spans multiple lines."; ?>Risultato: This text spans multiple lines. Str_repeat • La funzione ripete per un certo numero di volte un certo • Example • <?php echo str_repeat(".",13); ?>The output of the code above will be: • ............. Str_replace • La funzione sostituisce alcuni caratteri con altri caratteri • • • • in una stringa Esempio <?php echo str_replace("world","Peter","Hello world!"); ?> The output of the code above will be: Hello Peter! Strpos La funzione restituisce la posizione della prima occorrenza di una stringa all’interno di un’altra stringa Esempio • <?php echo strpos("Hello world!","wo"); ?> • The output of the code above will be: •6 Strlen • Questa funzione restituisce la lunghezza di una stringa • Example • <?php echo strlen("Hello world!"); ?>The output of the code above will be: • 12 Strrpos • Trova la posizione dell’ultima occorrezza di una stringa dentro un’altra stringa • Example • <?php echo strrpos("Hello world!","wo"); ?> • The output of the code above will be: •6 Substr • La funzione restituisce una parte di una stringa • Esempio 1 • <?php • • • • • echo substr("Hello world!",6); ?> The output of the code above will be: world! Esempio2 <?php echo substr("Hello world!",6,5); ?>The output of the code above will be: world • • • • • • • • • • • • • • The substr_compare() function compares two strings from a specified start position. This function returns: 0 - if the two strings are equal <0 - if string1 (from startpos) is less than string2 >0 - if string1 (from startpos) is greater than string2 Esempio1 <?php echo substr_compare("Hello world","Hello world",0); ?> The output of the code above will be: 0 Esempio2 <?php echo substr_compare("Hello world","world",6); ?>The output of the code above will be: 0 Esempio 3 <?php echo substr_compare("Hello world","WORLD",6,TRUE); ?>The output of the code above will be: Substr_count() The substr_count() function counts the number of times a substring occurs in a string. • Example • <?php echo substr_count("Hello world. The world is nice","world"); ?>The output of the code above will be: •2 Trim • The trim() function removes whitespaces and other predefined characters • • • • • from both sides of a string. Syntax <html> <body> <?php $str = " Hello World! "; echo "Without trim: " . $str; echo "<br />"; echo "With trim: " . trim($str); ?> <body> <html>The browser output of the code above will be: Without trim: Hello World! With trim: Hello World! If you select "View source" in the browser window, you will see the following HTML: <html> <body> Without trim: Hello World! <br />With trim: Hello World! </body> </html> Strtolower() • Converte tutti i caratteri di stringa in minuscolo • <?php echo strtolower("Hello WORLD."); ?> • Il risultato di questo codice php sarà: • hello world. Strtoupper() • Converte tutti i caratteri di una stringa in maiuscolo • <?php echo strtoupper("Hello WORLD!"); ?> • Il risultato del codice soprascritto sarà: HELLO WORLD!