Php preg_替换为两个数组
我在使用数组时遇到了一个问题 基本上,我想转置这个字符串Php preg_替换为两个数组,php,arrays,preg-replace,Php,Arrays,Preg Replace,我在使用数组时遇到了一个问题 基本上,我想转置这个字符串 $string = "Dm F Bb F Am"; 到 以下是我的工作: $Find = Array("/Dm/", "/F/", "/Bb/", "/Am/", "/Bbm/", "/A/", "/C/"); $Replace = Array('F#m', 'A', 'D', 'C#m', 'Dm', 'C#', 'E'); $New_string = preg_replace($Find, $Replace, $string);
$string = "Dm F Bb F Am";
到
以下是我的工作:
$Find = Array("/Dm/", "/F/", "/Bb/", "/Am/", "/Bbm/", "/A/", "/C/");
$Replace = Array('F#m', 'A', 'D', 'C#m', 'Dm', 'C#', 'E');
$New_string = preg_replace($Find, $Replace, $string);
但我得到的结果是:
E#m E#D E#E#m
问题是,每一个匹配都被以下内容替换,类似的情况会发生(例如E##m):
Dm->F#m->A#m->C#m->E#m
有没有办法简单地将“Dm”改为“F#m”,将“F”改为“A”等
谢谢大家! 您可以使用:
您可以使用:
preg\u replace\u callback()
可能是最简单的方法,您需要在单个操作中完成。大概是这样的:
<?php
$string = "Dm F Bb F Am";
$replacements = array (
'Dm' => 'F#m',
'F' => 'A',
'Bb' => 'D',
'Am' => 'C#m',
'Bbm' => 'Dm',
'A' => 'C#',
'C' => 'E'
);
$New_string = preg_replace_callback('/\b('.implode('|', array_map('preg_quote', array_keys($replacements), array_fill(0, count($replacements), '/'))).')\b/', function($match) use($replacements) {
return $replacements[$match[1]];
}, $string);
echo $New_string;
preg\u replace\u callback()
可能是最简单的方法,您需要在单个操作中完成。大概是这样的:
<?php
$string = "Dm F Bb F Am";
$replacements = array (
'Dm' => 'F#m',
'F' => 'A',
'Bb' => 'D',
'Am' => 'C#m',
'Bbm' => 'Dm',
'A' => 'C#',
'C' => 'E'
);
$New_string = preg_replace_callback('/\b('.implode('|', array_map('preg_quote', array_keys($replacements), array_fill(0, count($replacements), '/'))).')\b/', function($match) use($replacements) {
return $replacements[$match[1]];
}, $string);
echo $New_string;
看起来像一个简单的str_replace()
对我来说,不需要preg
谢谢,但我已经尝试了str_replace()
,结果完全一样,是错误的……哦,对了,我明白你的意思了。将在一秒钟内回答。我会尝试将字符串分解成数组,并在forEach循环中逐个替换每个项。对我来说,这看起来像一个简单的str_replace()
,不需要preg
谢谢,但我已经尝试了str_replace()
,结果完全相同,错了…哦,对了,我明白你的意思了。我会尝试将字符串分解成一个数组,并在forEach循环中逐个替换每个项目。啊,是的,我忘了你可以用螺丝刀而不是锤子拧螺丝+1@DaveRandom你今天让我笑了。顺便说一句,程序员节快乐:谢谢你,你救了一条命:)啊,是的,我忘了你可以用螺丝刀代替锤子来拧螺丝+1@DaveRandom你今天让我笑了。顺便说一句,程序员节快乐:谢谢你,你救了一条命:)
<?php
$string = "Dm F Bb F Am";
$replacements = array (
'Dm' => 'F#m',
'F' => 'A',
'Bb' => 'D',
'Am' => 'C#m',
'Bbm' => 'Dm',
'A' => 'C#',
'C' => 'E'
);
$New_string = preg_replace_callback('/\b('.implode('|', array_map('preg_quote', array_keys($replacements), array_fill(0, count($replacements), '/'))).')\b/', function($match) use($replacements) {
return $replacements[$match[1]];
}, $string);
echo $New_string;
// The input string and a map of search => replace
$string = "Dm F Bb F Am";
$replacements = array (
'Dm' => 'F#m',
'F' => 'A',
'Bb' => 'D',
'Am' => 'C#m',
'Bbm' => 'Dm',
'A' => 'C#',
'C' => 'E'
);
// Get a list of the search strings only
$searches = array_keys($replacements);
// Fill an array with / characters to the same length as the number of search
// strings. This is required for preg_quote() to work properly
$delims = array_fill(0, count($searches), '/');
// Apply preg_quote() to each search string so it is safe to use in the regex
$quotedSearches = array_map('preg_quote', $searches, $delims);
// Build the regex
$expr = '/\b('.implode('|', $quotedSearches).')\b/';
// Define a callback that will translate search matches to replacements
$callback = function($match) use($replacements) {
return $replacements[$match[1]];
};
// Do the replacement
$New_string = preg_replace_callback($expr, $callback, $string);