<?php
$original_chars = array(
'/A/','/B/','/C/'
);
$replaced_chars = array(
'a','b','c'
);
$updated_filename = preg_replace( $original_chars, $replaced_chars, $filename );
?>
I need to combine two arrays with chars into one associative array.
How should I rewrite the line with preg_replace from previous code sample?
<?php
$array_chars = array(
'/A/' => 'a',
'/B/' => 'b',
'/C/' => 'c'
);
//$updated_filename = preg_replace( $original_chars, $replaced_chars, $filename );
?>
您应该能够使用array_keys
和array_values
(未经测试)
$updated_filename = preg_replace( array_keys($original_chars), array_values($replaced_chars), $filename );
First at all the best way to do that is strtr()
$filename = strtr($filename, "ABC", "abc");
or
$array_chars('A' => 'a', 'B' => 'b', 'C' => 'c');
$filename = strtr($filename, $array_chars);
For the use of preg_replace with an associative array, you must use array_keys()
:
$result = preg_replace(array_keys($array_chars), $array_chars, $filename);
(note that this way is not very usefull and that array_values()
is not needed.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.