Domanda

Cosa c'è di sbagliato nel mio codice:

$i = new RegexIterator(
  new ArrayIterator(array(
    'test1'=>'test888', 
    'test2'=>'what?', 
    'test3'=>'test999')),
  '/^test(.*)/',
  RegexIterator::REPLACE);

foreach ($i as $name=>$value)
  echo $name . '=>' . $value . "\n";

L'iteratore è vuoto, perché? Grazie per l'aiuto!

È stato utile?

Soluzione 2

Come già detto, si tratta di un bug in PHP. Ho riferito a php.net: http://bugs.php.net/bug. php? id = 50579

Altri suggerimenti

Se si ommit la modalità di funzionamento (3 ° parametro nella tua nuova dichiarazione RegexIterator) otterrai i valori corrispondenti, in questo modo:

$array = array('test1' => 'test888', 'test2' => 'what?', 'test3' => 'test999');
$pattern = '/^test(.*)/';

echo '<pre>';
echo "DEFAULT\n";
$arrayIterator = new ArrayIterator($array);
$regexIterator = new RegexIterator($arrayIterator, $pattern);
foreach ($regexIterator as $value) {echo "$value\n";}
echo '</pre>';

Si può giocare con le diverse modalità di funzionamento, a seconda di ciò che si desidera. Go leggere su documentazione setMode: http://www.php.net/ manuale / it / regexiterator.setmode.php

Si consideri il seguente codice

$mixedArray=array(
    'tester2',
    'tes1',
    'bad4',
    '2good2',
    '2birds',
    'birds8',
    '8young girls',
    '6 young boys'
);


$ait=new ArrayIterator($mixedArray);
$regexIt=new RegexIterator($ait,'/^(\d+)(\w+)/', RegexIterator::REPLACE);
$regexIt->replacement='$2:$1';

foreach($regexIt as $key=>$value){
    echo $value."<br>";
}

Output

good2:2
birds:2
young:8 girls
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top