Estrarre stringhe tra parentesi graffe

di cicco7 il
2 risposte
La {mia casa} è {molto} grande

Da questa stringa vorrei estrarre le sotto stringhe tra parentesi graffe.
Cioè vorrei ottenere un array contenente solo le sotto stringhe tra parentesi graffe:
- mia casa
- molto

So che dovrei usare preg_match() ma non so come impostare la regex.
Un aiuto?

2 Risposte

  • Ecco un esempio:
    
    <?php
    $mystring = 'La {mia casa} è {molto} grande';
    
    preg_match_all('/(?<={)(.*?)(?=})/', $mystring, $matches);
    var_dump($matches);
    /* Output:
    
    array(2) {
      [0]=>
      array(2) {
        [0]=>
        string(8) "mia casa"
        [1]=>
        string(5) "molto"
      }
      [1]=>
      array(2) {
        [0]=>
        string(8) "mia casa"
        [1]=>
        string(5) "molto"
      }
    }
    
    */
    
    source: https://stackoverflow.com/a/1454936/874578
  • Thanks
Devi accedere o registrarti per scrivere nel forum
2 risposte