A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearmulti words PHP arrays replacement - page 1
User InfoPosts
multi words PHP arrays replacement#1
<?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);
?>


Ok.

So my question is, is there any way I can do like this

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


Thanks

posted date: 2008-12-14 10:06:00


Re: multi words PHP arrays replacement#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-14 10:06:01


Re: multi words PHP arrays replacement#3
You can use pipes for "Alternation":$patterns[0] = '/quick|lazy|dog/';

posted date: 2008-12-14 10:10:00


Re: multi words PHP arrays replacement#4
You could also just assign the array like so:$patterns = array('/quick/','/brown/','/fox/','lazy/',/dog/');which of course assign 0-4

posted date: 2008-12-14 11:32:00


Re: multi words PHP arrays replacement#5
why not use str_replace ?$output = str_replace(array('quick', 'brown', 'fox'), array('lazy', 'white', 'rabbit'), $input)

posted date: 2008-12-14 12:47:00


Re: multi words PHP arrays replacement#6
Someone who can should retag this with regex and perhaps remove arrays.

posted date: 2008-12-14 13:33:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.