Come manipolare le stringhe in Php - Parte 1

in #technology7 years ago

cop_steemit.jpg


Come manipolare le stringhe in Php - Parte 1


Dopo la lettura di questo post, sarai in grado di effettuare alcune operazioni per manipolare le stringhe in Php.


1. La lunghezza di una stringa

Per conoscere la lunghezza di una stringa, cioè il numero di caratteri che la compone, Php mette a disposizione la funzione strlen.

strlen(STRING);


Ad esempio:

$stringa = "Questo è un testo di prova";
$lunghezza_stringa = strlen($stringa);


La variabile $lunghezza_stringa contiene proprio la lunghezza della stringa $stringa.

Il risultato è un intero che, per esempio, è possibile utilizzare per poter effettuare altre operazioni. Se vogliamo realizzare una funzione che restituisce TRUE|FALSE in base alla lunghezza di una stringa:

function control($stringa, $soglia) {
    if(strlen($stringa)>$soglia) {
        return true;
    } else {
        return false;
    }
}


La funzione restituisce TRUE se la lunghezza della $stringa supera una particolare $soglia, altrimenti restituisce FALSE.


2. Sostituire le occorrenze di una stringa

str_replace è la funzione Php che consente di sostituire tutte le occorrenze di una porzione di stringa in un'altra stringa.

In generale, la funzione è così strutturata:

str_replace(SEARCH, REPLACE, SUBJECT, COUNT)


La funzione str_replace, quindi, ricerca tutte le occorrenze di SEARCH presenti nella stringa SUBJECT e le sostituisce con REPLACE. In COUNT (variabile opzionale) viene memorizzato il numero di occorrenze che vengono sostituite.

Ad esempio, se abbiamo:

$stringa = "Lorem ipsum dolor sit amet, Lorem ipsum...";


Se volessimo sostituire la porzione "ipsum" con "ipsom":

str_replace("ipsum", "ipsom", $stringa);

3. Suddividere o unire una stringa

Se ci fosse l'esigenza di suddividere una stringa in base ad un particolare carattere separatore, la funzione a disposizione è explode().

explode(SEPARATOR, STRING, LIMIT);


dove SEPARATOR è il carattere separatore, STRINGA è la stringa da suddividere e LIMIT è un parametro opzionale e indica il numero massimo di elementi da estrarre nella suddivisione.

Quindi, se abbiamo stringa del tipo:

$stringa = "testo1;testo2;testo3;testo4";


Volendo suddividere $stringa con il carattere separatore ;:

$suddivisione = explode(";", $stringa);


Il risultato contenuto nella variabile $suddivisione sarà un array del tipo:

$suddivisione = array(0 => "testo1", 1 => "testo2", 2 =>"testo3", 3 => "testo4");


Per recuperare gli elementi della suddivisione, occorrerà agire come si fa normalmente quando si hanno degli array. Ad esempio, se volessimo ciclare sugli elementi:

foreach($suddivisione as $elemento) {
    echo $elemento . " ";
}


oppure:

for($i=0; $i<count($suddivisione); $i++) {
    echo $suddivisione[$i] . " ";
}

Per effettuare l'operazione inversa, c'è la funzione implode().

implode(GLUE, PIECES)


dove PIECES è l'array contenente gli elementi da unire con il carattere separatore GLUE.

Volendo riunire l'array $suddivisione con il carattere separatore |:

$stringa = implode("|", $suddivisione);


Il risultato ottenuto sarà una stringa del tipo:

$stringa = "testo1|testo2|testo3|testo4";

4. Eliminare gli spazi ad inizio e fine stringa

Alcune funzioni molto semplici, consentono di eliminare eventuali spazi presenti ad inizio e/o fine stringa.

In generale:

trim(STRING)


Elimina gli spazi ad inizio e a fine stringa.

ltrim(STRING)


Elimina gli spazi ad inizio stringa.

rtrim(STRING)


Elimina gli spazi a fine stringa.

Se abbiamo la stringa:

$stringa = " Mario Rossi ";


Utilizzando quelle funzioni e applicandole a $stringa nel seguente modo:

trim($stringa);
ltrim($stringa);
rtrim($stringa);


Otterremo i risultati descritti in precedenza.



Ti sei perso le mini guide precedenti?


ilbarone623.png

Sort:  

Congratulations @ilbarone623! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of posts published
Award for the number of upvotes received

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Congratulations @ilbarone623! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

This post has been ranked within the top 50 most undervalued posts in the second half of Dec 07. We estimate that this post is undervalued by $24.78 as compared to a scenario in which every voter had an equal say.

See the full rankings and details in The Daily Tribune: Dec 07 - Part II. You can also read about some of our methodology, data analysis and technical details in our initial post.

If you are the author and would prefer not to receive these comments, simply reply "Stop" to this comment.

Coin Marketplace

STEEM 0.20
TRX 0.14
JST 0.030
BTC 67675.66
ETH 3248.40
USDT 1.00
SBD 2.66