Çok kelime PHP diziler yedek

3 Cevap php
<?php
$string = 'The quick brown fox jumped over the lazy dog.';
$patterns[0] = '/quick/';
$patterns[1] = '/brown/';
$patterns[2] = '/fox/';
$replacements[0] = 'slow';
$replacements[1] = 'black';
$replacements[2] = 'bear';
echo preg_replace($patterns, $replacements, $string);
?>

Ok guys, Now I have the above code. It just works well. Now for example I'd like to also replace "lazy" and "dog" with "slow" What I have to do now is would look like this, right?

<?php
$string = 'The quick brown fox jumped over the lazy dog.';
$patterns[0] = '/quick/';
$patterns[1] = '/brown/';
$patterns[2] = '/fox/';
$patterns[3] = '/lazy/';
$patterns[4] = '/dog/';
$replacements[0] = 'slow';
$replacements[1] = 'black';
$replacements[2] = 'bear';
$replacements[3] = 'slow';
$replacements[4] = 'slow';
echo preg_replace($patterns, $replacements, $string);
?>

Tamam.

Yani benim soru, böyle yapabilirim herhangi bir yolu yoktur

$patterns[0] = '/quick/', '/lazy/', '/dog/';
$patterns[1] = '/brown/';
$patterns[2] = '/fox/';
$replacements[0] = 'slow';
$replacements[1] = 'black';
$replacements[2] = 'bear';

Teşekkürler

3 Cevap

Sen boruları kullanabilirsiniz "Alternation":

$patterns[0] = '/quick|lazy|dog/';

neden str_replace kullanabilirsiniz değil?

$output = str_replace(array('quick', 'brown', 'fox'), array('lazy', 'white', 'rabbit'), $input)

Ayrıca sadece bu yüzden gibi dizi atayabilirsiniz:

$patterns = array('/quick/','/brown/','/fox/','lazy/',/dog/');

Hangi tabii 0-4 atamak